0

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));
Brownish Monster
  • 682
  • 1
  • 8
  • 25
  • Try removing the extra stuff in your Dependency Properties' setters. Just have then call `SetValue` and give it another shot. – Arian Motamedi Aug 28 '13 at 17:36
  • Still doesn't break. Is it supposed to break on the setters? Because the debugger doesn't break on the getters? I'm going to create a new test project and see if it is supposed to. – Brownish Monster Aug 28 '13 at 17:40
  • Does the converter ever get called? Note that when you use `Path` and `ElementName` in a binding, it'll look for that path in the specified element, not in the DataContext. So in the case of your CheckBox, it looks for `mwa.SortDirection`. What is `mwa` anyway? Is it your `MainWindow`? – Arian Motamedi Aug 28 '13 at 17:47
  • yh mwa is MainWindow and the Converter works for the ToggleButton, debugger stops at the breakpoint. I created a temp project and looks like the getters and setters are not called on bindings. – Brownish Monster Aug 28 '13 at 17:50
  • Wait I'm confused. So it works fine for the `ToggleButton` but not for the `ComboBox`? – Arian Motamedi Aug 28 '13 at 17:52
  • Only the ToggleButton has a converter and that works. When their properties change I want to call UpdateSort() but the debugger doesn't stop at their setters as it looks like it doesn't get called. Just realised I could set a `PropertyChangedCallback` in the code behind. – Brownish Monster Aug 28 '13 at 17:54

2 Answers2

0

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.

Seth Moore
  • 3,575
  • 2
  • 23
  • 33
0

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); }
}
dkozl
  • 32,814
  • 8
  • 87
  • 89