双向数据绑定不更新ComboBox&ToggleButton?

问题描述:

问题

我有一个ComboBoxMainWindow一个ToggleButton具有双向绑定自己的SelectedIndexIsChecked性质,分别。它们绑定的属性是DependencyProperties(DP),我在setter上有一个断点,但调试器从来没有停止过。我应该注意,这些绑定应该在DP上的初始者工作并且转换器也起作用。 VS的输出窗口也不值得关注。双向数据绑定不更新ComboBox&ToggleButton?

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> 

代码隐藏(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)); 
+0

尝试在你的依赖属性制定者去除多余的东西。只需调用'SetValue'并再次拍摄。 – PoweredByOrange

+0

仍然不会中断。它是否应该打破制定者的标准?因为调试器没有中断getters?我要创建一个新的测试项目,看看它是否应该。 –

+0

转换器是否被调用?请注意,当您在绑定中使用'Path'和'ElementName'时,它将在指定元素中查找该路径,而不是在DataContext中查找。所以在你的CheckBox的情况下,它会查找'mwa.SortDirection'。无论如何,“mwa”是什么?它是你的'MainWindow'吗? – PoweredByOrange

它不会中断,因为WPF将直接拨打GetValueSetValueDependencyProperty。如果您想在属性发生变化,那么你需要定义回调财产所有人将财产变做一些事情:

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

你实际调用的制定者?

例如,说

SortDirection = someOtherSortDirection; 

会进入你的二传手,但

SortDirection.SomeProperty = something; 

实际上是通过你的getter去。

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

+0

调试器也不停止在getters上。 –