寻找带有复选框的 WPF ComboBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/859227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Looking for a WPF ComboBox with checkboxes
提问by AngryHacker
My google skills fail me. Anyone heard of a control like that for WPF. I am trying to make it look like this (winforms screenshot).
我的谷歌技能让我失望。任何人都听说过 WPF 这样的控件。我试图让它看起来像这样(winforms 截图)。
回答by Martin Harris
You can do this yourself by setting the DataTemplate of the combo box. This articleshows you how - for a listbox, but the principle is the same.
您可以通过设置组合框的 DataTemplate 自己完成此操作。本文向您展示了如何 - 对于列表框,但原理是相同的。
Another article hereis perhaps a better fit for what you are trying to do, simple set the first column of the item template to be a checkbox and bind it to a bool on your business object.
此处的另一篇文章可能更适合您尝试执行的操作,只需将项目模板的第一列设置为复选框并将其绑定到业务对象上的 bool。
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}"
Width="20" />
<TextBlock Text="{Binding DayOfWeek}"
Width="100" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
回答by Sergey
There is my combobox. I use Martin Harris code and code from this link Can a WPF ComboBox display alternative text when its selection is null?
有我的组合框。我使用 Martin Harris 代码和此链接中的代码WPF ComboBox 在其选择为空时是否可以显示替代文本?
<ComboBox Name="cbObjects" Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Margin="2,2,6,0" SelectionChanged="OnCbObjectsSelectionChanged" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}" Width="20" VerticalAlignment="Center" Checked="OnCbObjectCheckBoxChecked" Unchecked="OnCbObjectCheckBoxChecked" />
<TextBlock Text="{Binding ObjectData}" VerticalAlignment="Center" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock IsHitTestVisible="False" Name="tbObjects" Text="Выберите объекты..." Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Margin="6,2,6,0" />
Small class for datasource:
数据源的小类:
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;
}
}
And there is two handler - one for handling CheckBox clicked and one for forming Text for ComboBox.
并且有两个处理程序 - 一个用于处理单击的 CheckBox,另一个用于为 ComboBox 形成文本。
private void OnCbObjectCheckBoxChecked(object sender, RoutedEventArgs e) {
StringBuilder sb = new StringBuilder();
foreach (SelectableObject<tblObject> cbObject in cbObjects.Items)
{
if (cbObject.IsSelected)
sb.AppendFormat("{0}, ", cbObject.ObjectData.Description);
}
tbObjects.Text = sb.ToString().Trim().TrimEnd(',');
}
private void OnCbObjectsSelectionChanged(object sender, SelectionChangedEventArgs e) {
ComboBox comboBox = (ComboBox)sender;
comboBox.SelectedItem = null;
}
For ComboBox.ItemsSource I use
对于 ComboBox.ItemsSource 我使用
ObservableCollection<SelectableObject<tblObject>>
where tblObject is type of my object, a list of which I want to display in ComboBox.
其中 tblObject 是我的对象的类型,我想在 ComboBox 中显示的列表。
I hope this code is useful to someone!
我希望这段代码对某人有用!
回答by Alex Klaus
Give a try to CheckComboBoxfrom Extended WPF Toolkit. The main advantage for me is having two lists for binding:
尝试从Extended WPF Toolkit 使用CheckComboBox。对我来说,主要优点是有两个绑定列表:
- all items available for selection
- just selected items
- 所有可供选择的项目
- 刚刚选择的项目
I find this approach more practical. In addition you can specify value
and display
members of the collections you're binding.
我觉得这种方法更实用。此外,你可以指定value
与display
你绑定集合的成员。
If you don't want to bring a bunch of other controls with CheckComboBox
, you can get the source codeof it, it's pretty straightforward (need to bring Selector classas well).
如果你不想带一堆其他控件CheckComboBox
,你可以得到它的源代码,它非常简单(也需要带Selector 类)。