2014-07-15 174 views
0

我正在尋找帶有複選框的WPF組合框。我回答了下面的鏈接並使用了Sergey提供的解決方案。 Looking for a WPF ComboBox with checkboxes帶複選框的WPF組合框

解決方案提供的工作正常,但我不知道如何綁定到組合框完成。我是WPF的新手,所以如果我可以得到任何幫助,這將是非常好的。 XAML代碼:

<Window x:Class="WpfApplication1.StackCombo" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="StackCombo" Height="338" Width="533"> 
<Grid> 
    <ComboBox Name="cbObjects" VerticalAlignment="Center" Margin="103,140,280,138" SelectionChanged="OnCbObjectsSelectionChanged" > 
     <ComboBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <CheckBox IsChecked="{Binding IsSelected}" Width="20" VerticalAlignment="Center" Checked="OnCbObjectCheckBoxChecked" Unchecked="OnCbObjectCheckBoxChecked" /> 
        <TextBlock Text="{Binding}" VerticalAlignment="Center" /> 
       </StackPanel> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
    </ComboBox> 
    <TextBlock IsHitTestVisible="False" Name="tbObjects" Text="None" VerticalAlignment="Center" Margin="113,100,268,184" /> 
</Grid> 

代碼背後: 公共部分類StackCombo:窗口 {

public StackCombo() 
    { 
     ObservableCollection<SelectableObject<Person>> _list; 
     InitializeComponent(); 
     _list = new ObservableCollection<SelectableObject<Person>>();    
     _list.Add(new SelectableObject<Person>(new Person("DEF")));    
     cbObjects.ItemsSource = _list;         
    } 

    private class Person 
     { 
      public Person(String firstName) 
      { 
       this.firstName=firstName;      
      } 
      private String firstName;   
      public String FirstName 
      { 
       get { return firstName; } 
       set { firstName = value; } 
      }    
     } 

    public class SelectableObject<T> 
    { 
     public bool IsSelected { get; set; } 
     public T ObjectData { get; set; } 

     public SelectableObject(T objectData) 
     { 
      ObjectData = objectData; 
     } 

     public SelectableObject(T objectData, bool isSelected) 
     { 
      IsSelected = isSelected; 
      ObjectData = objectData; 
     } 
    } 

    private void OnCbObjectCheckBoxChecked(object sender, RoutedEventArgs e) 
    { 
     StringBuilder sb = new StringBuilder(); 
     foreach (SelectableObject<Person> cbObject in cbObjects.Items) 
      if (cbObject.IsSelected) 
       sb.AppendFormat("{0}, ", cbObject.ObjectData.FirstName); 
     tbObjects.Text = sb.ToString().Trim().TrimEnd(','); 
    } 

    private void OnCbObjectsSelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     ComboBox comboBox = (ComboBox)sender; 
     comboBox.SelectedItem = null; 
    } 
} 

感謝, Ramkumar

+0

結合你需要使用的ItemSource,您使用的MVVM模式? –

+1

如果您想使用WPF,理解數據綁定的工作方式至關重要。在開始編寫WPF程序之前,您應該先閱讀WPF書籍以瞭解所有基本知識。至少您應該閱讀MSDN上的[數據綁定概述](http://msdn.microsoft.com/en-us/library/ms752347.aspx)文章。 – Clemens

+0

@ArijitMukherjee是的我使用ItemSource,但我得到綁定到組合框,而不是值的對象。是的,我使用MVVM模式。 –

回答

0

更改代碼:

<ComboBox Name="cbObjects" VerticalAlignment="Center" Margin="103,140,280,138" SelectionChanged="OnCbObjectsSelectionChanged" > 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <CheckBox IsChecked="{Binding IsSelected}" Width="20" VerticalAlignment="Center" Checked="OnCbObjectCheckBoxChecked" Unchecked="OnCbObjectCheckBoxChecked" /> 
       <TextBlock Text="{Binding}" VerticalAlignment="Center" /> 
      </StackPanel> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox> 

<ComboBox Name="cbObjects" VerticalAlignment="Center" Margin="103,140,280,138" SelectionChanged="OnCbObjectsSelectionChanged" > 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <CheckBox Content="{Binding FirstName}" IsChecked="{Binding IsSelected}" Width="20" VerticalAlignment="Center" Checked="OnCbObjectCheckBoxChecked" Unchecked="OnCbObjectCheckBoxChecked" /> 
       <TextBlock Text="{Binding}" VerticalAlignment="Center" /> 
      </StackPanel> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox> 

僅供參考如下:

Code Project Article

+0

如上所述,在更改代碼時出現「綁定約束」異常。從代碼中您可以看到FirstName存在於SelectableObject類的ObjectData下。 –

+0

我在下面的鏈接中使用了Sergey提出的解決方案。 http://stackoverflow.com/questions/859227/looking-for-a-wpf-combobox-with-checkboxes –

+0

你試過把FirstName改成ObjectData.FirstName嗎? –