Question

Problem

I have a ComboBox and a ToggleButton on MainWindow with two-way bindings on their SelectedIndex and IsChecked properties, respectively. The properties they bind to are DependencyProperties (DP) and I have a breakpoint on the setters but the debugger never stops at either. I should note that the bindings should work as the Initialisers on the DPs work and converters also work. Also nothing of concern is VS's Output Window.

XAML

<ToggleButton x:Name="tbSortDirection" Width="25" IsChecked="{Binding Path=SortDirection,Converter={StaticResource LDB},Mode=TwoWay,ElementName=mwa,UpdateSourceTrigger=PropertyChanged}">
    <ed:RegularPolygon Fill="#FF080808" Height="5" UseLayoutRounding="True" Margin="-2,0,0,0" PointCount="3" Width="6"/>
</ToggleButton>                 
<ComboBox x:Name="cbSort" Width="100" VerticalAlignment="Stretch" Margin="-5,0,0,0"  SelectedIndex="{Binding SelSortIndex,Mode=TwoWay,ElementName=mwa,UpdateSourceTrigger=PropertyChanged}" IsSynchronizedWithCurrentItem="True" >
    <ComboBoxItem Content="a"/>
    <ComboBoxItem Content="b"/>
    <ComboBoxItem Content="v"/>
    <ComboBoxItem Content="f"/>
</ComboBox> 

Code-Behind (DPs)

public ListSortDirection SortDirection
{
    get { return (ListSortDirection)GetValue(SortDirectionProperty); }
    set // BreakPoint here
    {
        MessageBox.Show("");
        SetValue(SortDirectionProperty, value);
        UpdateSort();
    }
}

public static readonly DependencyProperty SortDirectionProperty =
    DependencyProperty.Register("SortDirection", typeof(ListSortDirection), typeof(MainWindow), new PropertyMetadata(ListSortDirection.Ascending));



public int SelSortIndex
{
    get { return (int)GetValue(SelSortIndexProperty); }
    set // BreakPoint here
    {
        MessageBox.Show("");
        SetValue(SelSortIndexProperty, value);
        UpdateSort();
    }
}

public static readonly DependencyProperty SelSortIndexProperty =
    DependencyProperty.Register("SelSortIndex", typeof(int), typeof(MainWindow), new PropertyMetadata(1));
Was it helpful?

Solution

It will not break as WPF will call directly GetValue and SetValue on DependencyProperty. If you want to do something when property changes then you need to define callback for when property is changed:

public static readonly DependencyProperty SortDirectionProperty =
    DependencyProperty.Register("SortDirection", 
                                 typeof(ListSortDirection), 
                                 typeof(MainWindow), 
                                 new PropertyMetadata(ListSortDirection.Ascending, SortDirectionPropertyChangedCallback));

private static void SortDirectionPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
   (d as MainWindow).SortDirectionPropertyChangedCallback(e);
}

private void SortDirectionPropertyChangedCallback(DependencyPropertyChangedEventArgs e)
{
   UpdateSort();
}

public ListSortDirection SortDirection
{
    get { return (ListSortDirection)GetValue(SortDirectionProperty); }
    set { SetValue(SortDirectionProperty, value); }
}

OTHER TIPS

Are you actually calling the setters?

For instance, saying

SortDirection = someOtherSortDirection;

would go into your setter, but

SortDirection.SomeProperty = something;

actually goes through your getter.

Set a breakpoint in your getter and I wouldn't be surprised if it's being called when you think your setter should be.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top