2011-12-16 53 views
0

我有一個檢查列表框,並且我希望複選框是多重選擇的,但列表項目上的高亮條是單選。使用下面列出的代碼,複選框是多選的,但是在列表框中沒有高亮條,也不能從列表框中告訴當前選中的項。 (lbProtocols.SelectedItems始終爲0)沒有突出顯示的項目在我的已檢查列表框中

我錯過了什麼?

下面是列表框的XAML:

<ListBox Name ="lbProtocols" ItemsSource="{Binding AvailableProtocols}"> 
    <ListBox.ItemTemplate> 
    <HierarchicalDataTemplate> 
     <CheckBox Content="{Binding ProtocolName}" IsChecked="{Binding IsChecked}" /> 
    </HierarchicalDataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

這裏是後面的代碼結合我觀察到的集合到列表框中:

public ObservableCollection<CheckedListItem> AvailableProtocols; 

    AvailableProtocols = new ObservableCollection<CheckedListItem>(); 
    CheckedListItem item1 = new CheckedListItem(0, "first", false); 
item1.IUPropertyChanged += new PropertyChangedEventHandler(item_IUPropertyChanged); 
    CheckedListItem item2 = new CheckedListItem(1, "second", true); 
item2.IUPropertyChanged += new PropertyChangedEventHandler(item_IUPropertyChanged); 
    CheckedListItem item3 = new CheckedListItem(3, "third", false); 
item3.IUPropertyChanged += new PropertyChangedEventHandler(item_IUPropertyChanged); 
    AvailableProtocols.Add(item1); 
    AvailableProtocols.Add(item2); 
    AvailableProtocols.Add(item3); 

lbProtocols.ItemsSource = AvailableProtocols; 

這裏是CheckedListItem類:

public class CheckedListItem : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    public event PropertyChangedEventHandler IUPropertyChanged; 

    public CheckedListItem(){ } 
    public CheckedListItem(int id, string name, bool check) 
    { 
     ID = id; 
     ProtocolName = name; 
     IsChecked = check; 
    } 

    public int ID { get; set; } 
    public string ProtocolName { get; set; } 

    private bool _IsChecked; 

    public void SetCheckedNoNotify(bool check) 
    { 
     _IsChecked = check; 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs("IsChecked")); 
    } 

    public bool IsChecked 
    { 
     get { return _IsChecked; } 
     set 
     { 
      _IsChecked = value; 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs("IsChecked")); 
      if (IUPropertyChanged != null) 
       IUPropertyChanged(this, new PropertyChangedEventArgs("IsChecked")); 
     } 
    } 
} 
+0

是否有您所使用HeirarchicalDataTemplate代替DataTemplate的理由嗎?另外,如果您從代碼隱藏中設置ItemsSource,則無需在XAML中設置綁定。 – 2011-12-16 18:26:13

回答

1

在ListBoxItem中使用複選框時,您的問題很常見。該複選框正在處理單擊事件,因此從未通知您選擇該項目的列表框。如果您點擊每個項目旁邊文本的右側,您將看到該行被選中。

我可以建議最好的解決辦法是,你拆你的DataTemplate這樣

<DataTemplate> 
        <StackPanel Orientation="Horizontal"> 
        <CheckBox IsChecked="{Binding IsChecked}" /> 
        <TextBlock Text="{Binding ProtocolName}" /> 
        </StackPanel> 
       </DataTemplate> 

現在單擊文本選擇在列表框中的項目。如果你想複選框點擊使項目成爲選擇你必須綁定到的PropertyChanged在代碼隱藏複選框項,然後設置

lbProtocols.SelectedItem = sender 
+0

甜!這比我想出的解決方案更好,它只是在item_IUPropertyChanged處理程序中手動設置lbProtocols.SelectedItem。我喜歡你的解決方案的功能。謝謝! – 2011-12-16 19:48:47

相關問題