2011-06-07 81 views
0

我有一個ListBox中的複選框。我將ListBox ItemsSource設置爲代理列表。代理有一個屬性幫助DataBinding複選框WPF

public class Agency 
{ 
    public bool isSelected { get; set;} 
} 
<ListBox> <!-- ItemsSource set in codebehind to List<Agency> --> 
    <CheckBox IsChecked="{Binding Path=isSelected, Mode=TwoWay}" /> 
</ListBox> 

我有一個函數來檢查所有的複選框

//SelectAll button 

    private void SelectAll_Click(object sender, RoutedEventArgs e) 
    { 
     List<Agency> list = this.AgencySubListBox.ItemsSource as List<Agency>; 
     for (int i = 0; i < list.Count; i++) 
     { 
     Agency d = list[i]; 
     d.isSelected = true; 
     } 
    } 

當我打的全選按鈕,我希望複選框全部進行檢查。但沒有任何反應。

回答

1

您必須爲您的代理級執行INotifyPropertyChanged。 然後在您的isSelected-Property中,如果屬性的值已更改,則調用PropertyChanged。您在示例中使用的Auto-properties不支持INotifiyPropertyChanged,因此您不能將它們用於您的目的。 如果你使用.net,我也建議使用大寫字母來啓動屬性名稱。這是被廣泛接受的標準。