Note that there are some explanatory texts on larger screens.

plurals
  1. POProperties not updated on WPF control initialization
    primarykey
    data
    text
    <p>I'm new to WPF, and am having trouble getting the values for properties for a custom user control from the MainWindow XAML file.</p> <p>Here, I want to get the value "8" as the number of rows and columns but in my InitializeGrid() method, the properties are never set. They are always "0". What am I doing wrong?</p> <p>Any references will also be appreciated.</p> <hr> <p>This is my MainWindow.xaml (the relevant portions):</p> <pre><code>&lt;local:BoardView BoardRows="8" BoardColumns="8" /&gt; </code></pre> <p>This is my BoardView.xaml:</p> <pre><code>&lt;UniformGrid Name="uniformGrid" Rows="{Binding BoardRows}" Columns="{Binding BoardColumns}" &gt; &lt;/UniformGrid&gt; &lt;/UserControl&gt; </code></pre> <p>And this is my BoardView.xaml.cs:</p> <pre><code>[Description("The number of rows for the board."), Category("Common Properties")] public int BoardRows { get { return (int)base.GetValue(BoardRowsProperty); } set { base.SetValue(BoardRowsProperty, value); } } public static readonly DependencyProperty BoardRowsProperty = DependencyProperty.Register("BoardRows", typeof(int), typeof(UniformGrid)); [Description("The number of columns for the board."), Category("Common Properties")] public int BoardColumns { get { return (int)base.GetValue(BoardColumnsProperty); } set { base.SetValue(BoardColumnsProperty, value); } } public static readonly DependencyProperty BoardColumnsProperty = DependencyProperty.Register("BoardColumns", typeof(int), typeof(UniformGrid)); public BoardView() { InitializeComponent(); DataContext = this; InitializeGrid(); } private void InitializeGrid() { int rows = BoardRows; int cols = BoardColumns; for (int i = 0; i &lt; rows; i++) { for (int j = 0; j &lt; cols; j++) { uniformGrid.Children.Add( ... ); // ... } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload