Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Binding and DataTemplate
    primarykey
    data
    text
    <p>I have run into a problem in my app and I can't get past it. I have created the following simple WPF app to illustrate the situation.</p> <p>MainWindow.xaml:</p> <pre><code>&lt;Window x:Class="GlobalDataTemplate.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:my="clr-namespace:GlobalDataTemplate" Title="MainWindow" Height="350" Width="525"&gt; &lt;Window.Resources&gt; &lt;DataTemplate DataType="{x:Type my:MyData}"&gt; &lt;StackPanel Background="{Binding BgColor}"&gt; &lt;TextBlock Text="{Binding Text}"/&gt; &lt;TextBlock Text="{Binding Number}"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/Window.Resources&gt; &lt;ItemsControl&gt; &lt;ItemsControl.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;UniformGrid Columns="3" /&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ItemsControl.ItemsPanel&gt; &lt;my:MyData x:Name="NW" Text="NW" Number="1" BgColor="#FFFF0000" /&gt; &lt;my:MyData x:Name="N" Text="N" Number="2" BgColor="#FF63FF00" /&gt; &lt;my:MyData x:Name="NE" Text="NE" Number="3" BgColor="#FFFFCA00" /&gt; &lt;my:MyData x:Name="W" Text="W" Number="4" BgColor="#FF0037FF" /&gt; &lt;my:MyData x:Name="C" Text="C" Number="5" BgColor="#FF9E00FF" /&gt; &lt;my:MyData x:Name="E" Text="E" Number="6" BgColor="#FF838383" /&gt; &lt;my:MyData x:Name="SW" Text="SW" Number="7" BgColor="{Binding ElementName=NW, Path=BgColor}" /&gt; &lt;my:MyData x:Name="S" Text="S" Number="8" BgColor="{Binding ElementName=N, Path=BgColor}" /&gt; &lt;my:MyData x:Name="SE" Text="SE" Number="9" BgColor="{Binding ElementName=NE, Path=BgColor}" /&gt; &lt;/ItemsControl&gt; &lt;/Window&gt; </code></pre> <p>MyData.cs:</p> <pre><code>using System.Windows; using System.Windows.Media; namespace GlobalDataTemplate { class MyData : DependencyObject { public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MyData), new UIPropertyMetadata(null)); public int Number { get { return (int)GetValue(NumberProperty); } set { SetValue(NumberProperty, value); } } public static readonly DependencyProperty NumberProperty = DependencyProperty.Register("Number", typeof(int), typeof(MyData), new UIPropertyMetadata(0)); public Brush BgColor { get { return (Brush)GetValue(BgColorProperty); } set { SetValue(BgColorProperty, value); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty BgColorProperty = DependencyProperty.Register("BgColor", typeof(Brush), typeof(MyData), new UIPropertyMetadata(null)); } } </code></pre> <p>From the XAML, you would expect to see a 3x3 grid with the same colors across the bottom as shows across the top. But none of the colors for the bottom row show at all (you see the white background of the window). How can I get the colors at the bottom to bind to the colors at the top properly?</p> <p>I've also tried adding a property changed handler and setting a break point. The break point is never hit.</p> <p>Thanks in advance.</p>
    singulars
    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