Note that there are some explanatory texts on larger screens.

plurals
  1. PODependencyProperty binding not happening on initial load
    primarykey
    data
    text
    <p>I'm trying to do something simple -- make a DependencyProperty and then bind to it. However, the getter doesn't appear to fire when I start up the app. (I'm guessing the solution will make me smack my head and go "Doh!", but still. :) )</p> <p>Any ideas?</p> <p>Code-behind code:</p> <pre><code>public static readonly DependencyProperty PriorityProperty = DependencyProperty.Register("Priority", typeof (Priority), typeof (PriorityControl), null); public Priority Priority { get { return (Priority)GetValue(PriorityProperty); } set { SetValue(PriorityProperty, value); } } </code></pre> <p>Control XAML:</p> <pre><code>&lt;ListBox Background="Transparent" BorderThickness="0" ItemsSource="{Binding Path=Priorities}" Name="PriorityList" SelectedItem="{Binding Path=Priority, Mode=TwoWay}"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Grid Height="16" Width="16"&gt; &lt;Border BorderBrush="Black" BorderThickness="2" CornerRadius="3" Visibility="{Binding RelativeSource= {RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected, Converter={StaticResource boolToVisibilityConverter}}" /&gt; &lt;Border CornerRadius="3" Height="12" Width="12"&gt; &lt;Border.Background&gt; &lt;SolidColorBrush Color="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=Content, Converter={StaticResource priorityToColorConverter}}" /&gt; &lt;/Border.Background&gt; &lt;/Border&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;ListBox.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;StackPanel Orientation="Horizontal"/&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ListBox.ItemsPanel&gt; &lt;/ListBox&gt; </code></pre> <p>Binding statement:</p> <pre><code>&lt;UI:PriorityControl Grid.Column="8" Priority="{Binding Path=Priority}" VerticalAlignment="Center"/&gt; </code></pre> <p>Some other notes:</p> <ul> <li>Binding is in a UserControl</li> <li>UserControl contains the PriorityControl</li> <li>PriorityControl contains the DependencyProperty</li> <li>I've checked that the data the UserControl is getting the appropriate data -- every other binding works.</li> <li>If I change the selection on the PriorityControl via the mouse, everything fires as appropriate. It's just that initial setting of the value that isn't working.</li> <li>Priority is an enum.</li> </ul> <p><strong>EDIT:</strong> I've tried two additional things. First, I made the binding to the value two-way, but that didn't work. Then, I added a property changed callback to the dependency property and made it call OnPropertyChanged, like so:</p> <pre><code>public static readonly DependencyProperty PriorityProperty = DependencyProperty.Register("Priority", typeof (Priority), typeof (PriorityControl), new PropertyMetadata(HandleValueChanged)); private static void HandleValueChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { var npc = dependencyObject as PriorityControl; npc.OnPropertyChanged("Priority"); } </code></pre> <p>That didn't work either. I even tried overriding ApplyTemplate on the control to force a property change notification, but the value is never getting set on initial load. I can change values and see everything fire just fine, but it's that first time that nothing is coming through.</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.
 

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