Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. UserControl

UserControl

Scheduled Pinned Locked Moved WPF
csharpcsswpfwinformsdesign
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I'm creating a chess game. The board is made up of a grid of user controls called BoardSqaure:

    and

    namespace Chess.UI.WPF.Controls
    {
    public partial class BoardSquare : UserControl
    {
    public BoardSquare()
    {
    InitializeComponent();
    }

        public static readonly DependencyProperty PieceProperty =
                    DependencyProperty.Register("Piece",
                    typeof(PieceBase),
                    typeof(BoardSquare),
                    new PropertyMetadata(null));
    
    
        public PieceBase Piece
        {
            get { return (PieceBase)GetValue(PieceProperty); }
            set { SetValue(PieceProperty, value); }
        }
    }
    

    }

    The DP Piece will hold an instance of PieceBase, which can be King, Queen, Rook.. etc. To load the board in the code behind I'm trying to do

    private void SetupBoard()
    {
    // Do this for the first 2 and last 2 rows for initial set up
    BoardSquare square = board.Children.Cast().First(e => Grid.GetRow(e) == 0 && Grid.GetColumn(e) == 0);
    square.Piece = new RookBlack();

    square = board.Children.Cast().First(e => Grid.GetRow(e) == 0 && Grid.GetColumn(e) == 1);
    square.Piece = new BishopBlack();
    

    .
    .
    .

    }

    However the pieces never shows up. What am I doing wrong here?

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    J L M Richard DeemingR 4 Replies Last reply
    0
    • K Kevin Marois

      I'm creating a chess game. The board is made up of a grid of user controls called BoardSqaure:

      and

      namespace Chess.UI.WPF.Controls
      {
      public partial class BoardSquare : UserControl
      {
      public BoardSquare()
      {
      InitializeComponent();
      }

          public static readonly DependencyProperty PieceProperty =
                      DependencyProperty.Register("Piece",
                      typeof(PieceBase),
                      typeof(BoardSquare),
                      new PropertyMetadata(null));
      
      
          public PieceBase Piece
          {
              get { return (PieceBase)GetValue(PieceProperty); }
              set { SetValue(PieceProperty, value); }
          }
      }
      

      }

      The DP Piece will hold an instance of PieceBase, which can be King, Queen, Rook.. etc. To load the board in the code behind I'm trying to do

      private void SetupBoard()
      {
      // Do this for the first 2 and last 2 rows for initial set up
      BoardSquare square = board.Children.Cast().First(e => Grid.GetRow(e) == 0 && Grid.GetColumn(e) == 0);
      square.Piece = new RookBlack();

      square = board.Children.Cast().First(e => Grid.GetRow(e) == 0 && Grid.GetColumn(e) == 1);
      square.Piece = new BishopBlack();
      

      .
      .
      .

      }

      However the pieces never shows up. What am I doing wrong here?

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      J Offline
      J Offline
      Jon McKee
      wrote on last edited by
      #2

      Kevin Marois wrote:

      BoardSquare square = board.Children.Cast<BoardSquare>().First(e => Grid.GetRow(e) == 0 && Grid.GetColumn(e) == 0); square.Piece = new RookBlack();   square = board.Children.Cast<BoardSquare>().First(e => Grid.GetRow(e) == 0 && Grid.GetColumn(e) == 1); square.Piece = new BishopBlack();

      Well first off the bishop doesn't go next to the rook :-D In all seriousness though, I see nothing wrong with the code you've posted. Could you edit with all relevant Queen code? I'm assuming the other pieces are displaying properly?

      K 1 Reply Last reply
      0
      • J Jon McKee

        Kevin Marois wrote:

        BoardSquare square = board.Children.Cast<BoardSquare>().First(e => Grid.GetRow(e) == 0 && Grid.GetColumn(e) == 0); square.Piece = new RookBlack();   square = board.Children.Cast<BoardSquare>().First(e => Grid.GetRow(e) == 0 && Grid.GetColumn(e) == 1); square.Piece = new BishopBlack();

        Well first off the bishop doesn't go next to the rook :-D In all seriousness though, I see nothing wrong with the code you've posted. Could you edit with all relevant Queen code? I'm assuming the other pieces are displaying properly?

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        No piece that I assign to the Piece property shows up

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        J 1 Reply Last reply
        0
        • K Kevin Marois

          I'm creating a chess game. The board is made up of a grid of user controls called BoardSqaure:

          and

          namespace Chess.UI.WPF.Controls
          {
          public partial class BoardSquare : UserControl
          {
          public BoardSquare()
          {
          InitializeComponent();
          }

              public static readonly DependencyProperty PieceProperty =
                          DependencyProperty.Register("Piece",
                          typeof(PieceBase),
                          typeof(BoardSquare),
                          new PropertyMetadata(null));
          
          
              public PieceBase Piece
              {
                  get { return (PieceBase)GetValue(PieceProperty); }
                  set { SetValue(PieceProperty, value); }
              }
          }
          

          }

          The DP Piece will hold an instance of PieceBase, which can be King, Queen, Rook.. etc. To load the board in the code behind I'm trying to do

          private void SetupBoard()
          {
          // Do this for the first 2 and last 2 rows for initial set up
          BoardSquare square = board.Children.Cast().First(e => Grid.GetRow(e) == 0 && Grid.GetColumn(e) == 0);
          square.Piece = new RookBlack();

          square = board.Children.Cast().First(e => Grid.GetRow(e) == 0 && Grid.GetColumn(e) == 1);
          square.Piece = new BishopBlack();
          

          .
          .
          .

          }

          However the pieces never shows up. What am I doing wrong here?

          If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          There's too many things that you may have accounted for that we can't see: Width Height Visibility Transparency Z-Order Background color Foreground color Top Left Rendering ...

          1 Reply Last reply
          0
          • K Kevin Marois

            No piece that I assign to the Piece property shows up

            If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

            J Offline
            J Offline
            Jon McKee
            wrote on last edited by
            #5

            There's really no way to tell from the code posted what's going on. ContentPresenter is generally used in a ControlTemplate. Using it raw like this means there are SOO many variables that could affect what's going on. Is there a DataTemplate associated with the type? What type of element is PieceBase? Does it have any templates associated with it? ContentPresenter is a very dynamic class. You can see just how many different things it checks and may or may not apply in the remarks section of its MSDN page[^].

            K 1 Reply Last reply
            0
            • J Jon McKee

              There's really no way to tell from the code posted what's going on. ContentPresenter is generally used in a ControlTemplate. Using it raw like this means there are SOO many variables that could affect what's going on. Is there a DataTemplate associated with the type? What type of element is PieceBase? Does it have any templates associated with it? ContentPresenter is a very dynamic class. You can see just how many different things it checks and may or may not apply in the remarks section of its MSDN page[^].

              K Offline
              K Offline
              Kevin Marois
              wrote on last edited by
              #6

              OK, so what would be the right architecture here for a chess board? Keep in mind that the user will drag/drop pieces around, and pieces will also need to be programmatically added & removed. Thanks

              If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

              J 1 Reply Last reply
              0
              • K Kevin Marois

                OK, so what would be the right architecture here for a chess board? Keep in mind that the user will drag/drop pieces around, and pieces will also need to be programmatically added & removed. Thanks

                If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                J Offline
                J Offline
                Jon McKee
                wrote on last edited by
                #7

                You can programmatically remove anything. For drag and drop, the MSDN article Walkthrough: Enabling Drag and Drop on a User Control[^] has everything you need. As far as architecture, that's entirely a personal choice. You can derive your UserControl from ContentControl and use a ContentPresenter. You could use Image controls[^] inside each Grid square. I mean WPF is a huge and flexible architecture. EDIT: I forgot to mention, you can use the ContentPresenter like that - nothing inherently wrong with it. There's just not enough information for me to say why it might not be working without all the code that could affect it.

                1 Reply Last reply
                0
                • K Kevin Marois

                  I'm creating a chess game. The board is made up of a grid of user controls called BoardSqaure:

                  and

                  namespace Chess.UI.WPF.Controls
                  {
                  public partial class BoardSquare : UserControl
                  {
                  public BoardSquare()
                  {
                  InitializeComponent();
                  }

                      public static readonly DependencyProperty PieceProperty =
                                  DependencyProperty.Register("Piece",
                                  typeof(PieceBase),
                                  typeof(BoardSquare),
                                  new PropertyMetadata(null));
                  
                  
                      public PieceBase Piece
                      {
                          get { return (PieceBase)GetValue(PieceProperty); }
                          set { SetValue(PieceProperty, value); }
                      }
                  }
                  

                  }

                  The DP Piece will hold an instance of PieceBase, which can be King, Queen, Rook.. etc. To load the board in the code behind I'm trying to do

                  private void SetupBoard()
                  {
                  // Do this for the first 2 and last 2 rows for initial set up
                  BoardSquare square = board.Children.Cast().First(e => Grid.GetRow(e) == 0 && Grid.GetColumn(e) == 0);
                  square.Piece = new RookBlack();

                  square = board.Children.Cast().First(e => Grid.GetRow(e) == 0 && Grid.GetColumn(e) == 1);
                  square.Piece = new BishopBlack();
                  

                  .
                  .
                  .

                  }

                  However the pieces never shows up. What am I doing wrong here?

                  If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                  M Offline
                  M Offline
                  Meshack Musundi
                  wrote on last edited by
                  #8

                  In a chess app I wrote some time back: WPF: P2P Chess[^], I took an approach similar to what you're doing. I had a bunch of user controls to represent the pieces. Over time I grew uncomfortable with this and decided to take an MVVM friendly approach for an app I'm currently developing, where the user plays against Stockfish. The chess board in my current app is a ListBox whose ItemsPanel is a Grid with eight row and eight columns. The ItemsSource property of the ListBox is bound to a collection of objects that implement the same interface. The collection, in the View Model, is made up of BoardSquares and ChessPieces. The DataTemplate of the ListBox is an Image that uses MultiDataTriggers to determine the appropriate image to display depending on the type of object. I'll be posting an article soon, maybe tommorrow, where you can dig deeper into the code.

                  "As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"

                  K 1 Reply Last reply
                  0
                  • K Kevin Marois

                    I'm creating a chess game. The board is made up of a grid of user controls called BoardSqaure:

                    and

                    namespace Chess.UI.WPF.Controls
                    {
                    public partial class BoardSquare : UserControl
                    {
                    public BoardSquare()
                    {
                    InitializeComponent();
                    }

                        public static readonly DependencyProperty PieceProperty =
                                    DependencyProperty.Register("Piece",
                                    typeof(PieceBase),
                                    typeof(BoardSquare),
                                    new PropertyMetadata(null));
                    
                    
                        public PieceBase Piece
                        {
                            get { return (PieceBase)GetValue(PieceProperty); }
                            set { SetValue(PieceProperty, value); }
                        }
                    }
                    

                    }

                    The DP Piece will hold an instance of PieceBase, which can be King, Queen, Rook.. etc. To load the board in the code behind I'm trying to do

                    private void SetupBoard()
                    {
                    // Do this for the first 2 and last 2 rows for initial set up
                    BoardSquare square = board.Children.Cast().First(e => Grid.GetRow(e) == 0 && Grid.GetColumn(e) == 0);
                    square.Piece = new RookBlack();

                    square = board.Children.Cast().First(e => Grid.GetRow(e) == 0 && Grid.GetColumn(e) == 1);
                    square.Piece = new BishopBlack();
                    

                    .
                    .
                    .

                    }

                    However the pieces never shows up. What am I doing wrong here?

                    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                    Richard DeemingR Online
                    Richard DeemingR Online
                    Richard Deeming
                    wrote on last edited by
                    #9

                    Check the Visual Studio output window, and you'll probably find a load of binding errors telling you that the property Piece doesn't exist in the data context of the parent window. Update the user control so that it points to itself for the DataContext:

                    public BoardSquare()
                    {
                    InitializeComponent();
                    DataContext = this;
                    }

                    Or:

                    <UserControl x:Class="Chess.UI.WPF.Controls.BoardSquare"
                    ...
                    DataContext="{Binding RelativeSource={RelativeSource Self}}"


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    K 1 Reply Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      Check the Visual Studio output window, and you'll probably find a load of binding errors telling you that the property Piece doesn't exist in the data context of the parent window. Update the user control so that it points to itself for the DataContext:

                      public BoardSquare()
                      {
                      InitializeComponent();
                      DataContext = this;
                      }

                      Or:

                      <UserControl x:Class="Chess.UI.WPF.Controls.BoardSquare"
                      ...
                      DataContext="{Binding RelativeSource={RelativeSource Self}}"


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      K Offline
                      K Offline
                      Kevin Marois
                      wrote on last edited by
                      #10

                      You were right. Thanks!

                      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                      1 Reply Last reply
                      0
                      • M Meshack Musundi

                        In a chess app I wrote some time back: WPF: P2P Chess[^], I took an approach similar to what you're doing. I had a bunch of user controls to represent the pieces. Over time I grew uncomfortable with this and decided to take an MVVM friendly approach for an app I'm currently developing, where the user plays against Stockfish. The chess board in my current app is a ListBox whose ItemsPanel is a Grid with eight row and eight columns. The ItemsSource property of the ListBox is bound to a collection of objects that implement the same interface. The collection, in the View Model, is made up of BoardSquares and ChessPieces. The DataTemplate of the ListBox is an Image that uses MultiDataTriggers to determine the appropriate image to display depending on the type of object. I'll be posting an article soon, maybe tommorrow, where you can dig deeper into the code.

                        "As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"

                        K Offline
                        K Offline
                        Kevin Marois
                        wrote on last edited by
                        #11

                        I look forward to seeing it. One question... When setting up the board, the board is layout grid with 8 rows & 8 columns, and the pieces are added to the grid during setup. How do you reverse the board for the other player?

                        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                        M 1 Reply Last reply
                        0
                        • K Kevin Marois

                          I look forward to seeing it. One question... When setting up the board, the board is layout grid with 8 rows & 8 columns, and the pieces are added to the grid during setup. How do you reverse the board for the other player?

                          If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                          M Offline
                          M Offline
                          Meshack Musundi
                          wrote on last edited by
                          #12

                          Kevin Marois wrote:

                          How do you reverse the board for the other player?

                          By reverse I guess you mean flip. I rotate the ListBox and switch its DataTemplate. One DataTemplate has the Image control rotated at an angle of 0 while the other has the Image control rotated at an angle of 180 degrees.

                          "As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups