0

问题

我分别对它们的和ComboBox属性进行ToggleButtonMainWindow双向绑定。它们绑定的属性是 DependencyProperties (DP),我在设置器上有一个断点,但调试器永远不会停止。我应该注意,绑定应该像 DP 上的 Initialisers 一样工作,并且转换器也可以工作。VS 的输出窗口也没什么好担心的。SelectedIndexIsChecked

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> 

代码隐藏 (DP)

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));
4

2 回答 2

0

你真的在打电话给二传手吗?

比如说

SortDirection = someOtherSortDirection;

会进入你的二传手,但是

SortDirection.SomeProperty = something;

实际上通过你的吸气剂。

在你的 getter 中设置一个断点,如果你认为你的 setter 应该调用它,我不会感到惊讶。

于 2013-08-28T17:38:59.173 回答
0

它不会中断,因为 WPF 将GetValue直接SetValue调用DependencyProperty. 如果您想在属性更改时执行某些操作,则需要为属性更改时定义回调:

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); }
}
于 2013-08-28T17:57:41.820 回答