2013-08-28 35 views
0

問題

我有一個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

回答

0

它不會中斷,因爲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); } 
} 
0

你實際調用的制定者?

例如,說

SortDirection = someOtherSortDirection; 

會進入你的二傳手,但

SortDirection.SomeProperty = something; 

實際上是通過你的getter去。

在你的getter中設置一個斷點,如果你認爲你的setter應該是被調用的話,我不會感到驚訝。

+0

調試器也不停止在getters上。 –