2014-07-15 137 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吗? –