我正在寻找带复选框的WPF组合框。我参考了下面的链接并使用了Sergey给出的解决方案。 Looking for a WPF ComboBox with checkboxes
提供的解决方案工作正常但我不确定如何完成对组合框的绑定。我是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>
背后的代码: public partial class StackCombo:Window {
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 :(得分: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>
参考参考: