C# WPF:带有重置项的组合框
我想在WPF中创建一个组合框,该组合框顶部有一个C# WPF:带有重置项的组合框,c#,.net,wpf,xaml,combobox,C#,.net,Wpf,Xaml,Combobox,我想在WPF中创建一个组合框,该组合框顶部有一个null项,当选中该项时,SelectedItem应设置为null(重置为默认状态)。我一直在寻找,但没有找到令人满意的解决方案 如果可能的话,我希望它只使用XAML代码或附加的行为,因为我真的不喜欢为视图更改ViewModel中的内容,或者覆盖标准控件 以下是我到目前为止的想法(简称代码): […] (无) [...] 我认为最好的方法是以某种方式添加一个事件触发器,当项目被选中时,该触发器将SelectedIndex设置为-1,但这里是我遇
null
项,当选中该项时,SelectedItem应设置为null(重置为默认状态)。我一直在寻找,但没有找到令人满意的解决方案
如果可能的话,我希望它只使用XAML代码或附加的行为,因为我真的不喜欢为视图更改ViewModel中的内容,或者覆盖标准控件
以下是我到目前为止的想法(简称代码):
[…]
(无)
[...]
我认为最好的方法是以某种方式添加一个事件触发器,当项目被选中时,该触发器将SelectedIndex
设置为-1
,但这里是我遇到的问题
有什么办法吗?或者更好的方法,如附加行为?删除以下行并添加复选框,然后您可以执行自定义操作
<ComboBoxItem>(None)</ComboBoxItem>
(无)
考虑为“无”组合框项实现一个属性,并将该项添加到项目列表中。然后实现在该类中保存null对象的自定义逻辑,或者只检查所选项是否为null项类型。请使用以下代码
<ComboBoxItem IsSelected="{Binding ClearSelectedItems}">(None)</ComboBoxItem>
(无)
在viewmodel中,捕获“ClearSelectedItems”更改通知并清除Items控件的SelectedItems。如果选择了一个项目,则可以重置选择
<ComboBox x:Name="cb">
<ComboBox.Items>
<ComboBoxItem Content="(None)">
<ComboBoxItem.Triggers>
<EventTrigger RoutedEvent="Selector.Selected">
<BeginStoryboard>
<Storyboard Storyboard.TargetName="cb" Storyboard.TargetProperty="SelectedItem">
<ObjectAnimationUsingKeyFrames Duration="0:0:0">
<DiscreteObjectKeyFrame Value="{x:Null}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ComboBoxItem.Triggers>
</ComboBoxItem>
<ComboBoxItem>First Item</ComboBoxItem>
<ComboBoxItem>Second Item</ComboBoxItem>
</ComboBox.Items>
</ComboBox>
第一项
第二项
不幸的是,这将不适用于项资源和将此重置项添加到任意列表中的方法。原因是WPF无法解析此范围内的情节提要.TargetName
。
但这可能会帮助您继续重新部署组合框
对于此解决方案仍然不是100%满意,但到目前为止,我发现最好的一点是,您只需要覆盖组合框样式并应用附加的行为
<ComboBox ItemsSource="{Binding Names}"
ext:ComboBoxHelper.IsNullable="True" />
资料来源:
编辑:
链接到Internet存档,因为链接已断开:
对于类似的问题,我使用了以下解决方案。它利用绑定的Converter属性在内部表示(null是一个合理的值)和我希望在组合框中显示的内容之间来回切换。我喜欢不需要在模型或viewmodel中添加显式列表,但我不喜欢转换器中的字符串文字和组合框中的字符串文字之间的脆弱连接
<ComboBox SelectedValue="{Binding MyProperty, Converter={x:Static Converters:MyPropertySelectionConverter.Instance}}" >
<ComboBox.ItemsSource>
<CompositeCollection>
<sys:String>(none)</sys:String>
<CollectionContainer Collection="{Binding Source={x:Static Somewhere}, Path=ListOfPossibleValuesForMyProperty}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
虽然我同意WPF ComboBox的空项问题有很多解决方案,但启发我尝试一种不太过分的替代方案,它包括用空值(也被包装)包装每个源项允许将整个包装的集合注入到ComboBox.ItemsSource属性之前。所选项目将可用于SelectedWrappedItem属性
所以,首先定义通用包装器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComboBoxWrapperSample
{
/// <summary>
/// Wrapper that adds supports to null values upon ComboBox.ItemsSource
/// </summary>
/// <typeparam name="T">Source combobox items collection datatype</typeparam>
public class ComboBoxNullableItemWrapper<T>
{
string _nullValueText;
private T _value;
public T Value
{
get { return _value; }
set { _value = value; }
}
/// <summary>
///
/// </summary>
/// <param name="Value">Source object</param>
/// <param name="NullValueText">Text to be presented whenever Value argument object is NULL</param>
public ComboBoxNullableItemWrapper(T Value, string NullValueText = "(none)")
{
this._value = Value;
this._nullValueText = NullValueText;
}
/// <summary>
/// Text that will be shown on combobox items
/// </summary>
/// <returns></returns>
public override string ToString()
{
string result;
if (this._value == null)
result = _nullValueText;
else
result = _value.ToString();
return result;
}
}
}
定义您的视图模型
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
namespace ComboBoxWrapperSample
{
public partial class SampleViewModel : INotifyPropertyChanged
{
// SelectedWrappedItem- This property stores selected wrapped item
public ComboBoxNullableItemWrapper<Person> _SelectedWrappedItem { get; set; }
public ComboBoxNullableItemWrapper<Person> SelectedWrappedItem
{
get { return _SelectedWrappedItem; }
set
{
_SelectedWrappedItem = value;
OnPropertyChanged("SelectedWrappedItem");
}
}
// ListOfPersons - Collection to be injected into ComboBox.ItemsSource property
public ObservableCollection<ComboBoxNullableItemWrapper<Person>> ListOfPersons { get; set; }
public SampleViewModel()
{
// Setup a regular items collection
var person1 = new Person() { Name = "Foo", Age = 31 };
var person2 = new Person() { Name = "Bar", Age = 42 };
List<Person> RegularList = new List<Person>();
RegularList.Add(person1);
RegularList.Add(person2);
// Convert regular collection into a wrapped collection
ListOfPersons = new ObservableCollection<ComboBoxNullableItemWrapper<Person>>();
ListOfPersons.Add(new ComboBoxNullableItemWrapper<Person>(null));
RegularList.ForEach(x => ListOfPersons.Add(new ComboBoxNullableItemWrapper<Person>(x)));
// Set UserSelectedItem so it targes null item
this.SelectedWrappedItem = ListOfPersons.Single(x => x.Value ==null);
}
// INotifyPropertyChanged related stuff
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
}
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Linq;
名称空间ComboBoxWrapperSample
{
公共部分类SampleViewModel:INotifyPropertyChanged
{
//SelectedWrappedItem-此属性存储选定的包装项
公共ComboBoxNullableItemWrapper _SelectedWrappedItem{get;set;}
public ComboxNullableItemWrapper SelectedWrappedItem
{
获取{return _SelectedWrappedItem;}
设置
{
_SelectedWrappedItem=值;
OnPropertyChanged(“SelectedWrappedItem”);
}
}
//ListOfPersons-要注入ComboBox.ItemsSource属性的集合
公共ObservableCollection ListOfPersons{get;set;}
公共SampleViewModel()
{
//设置常规项目集合
var person1=newperson(){Name=“Foo”,年龄=31};
var person2=newperson(){Name=“Bar”,年龄=42};
List RegularList=新列表();
常规列表。添加(人员1);
常规列表。添加(人员2);
//将常规集合转换为包装集合
ListOfPersons=新的ObservableCollection();
Add(新的ComboBoxNullableItemWrapper(null));
ForEach(x=>listopersons.Add(newcomboboxNullableItemWrapper(x));
//设置UserSelectedItem,使其以空项为目标
this.SelectedWrappedItem=ListOfPersons.Single(x=>x.Value==null);
}
//InotifyProperty更改相关内容
公共事件属性更改事件处理程序属性更改;
受保护的void OnPropertyChanged(字符串名称)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
{
处理程序(此,新PropertyChangedEventArgs(名称));
}
}
}
}
最后是你的观点(好的,这是一个窗口)
最喜欢的老师
所选包装值:
说到这里,我是否提到可以通过SelectedWrappedItem.Value属性检索未包装的选定项
给你
希望它能帮助其他人以下是解决此问题的终极超级简单解决方案:
不要在ItemsSource中使用值为null的项,而是将DbNull.value用作项或项的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComboBoxWrapperSample
{
/// <summary>
/// Wrapper that adds supports to null values upon ComboBox.ItemsSource
/// </summary>
/// <typeparam name="T">Source combobox items collection datatype</typeparam>
public class ComboBoxNullableItemWrapper<T>
{
string _nullValueText;
private T _value;
public T Value
{
get { return _value; }
set { _value = value; }
}
/// <summary>
///
/// </summary>
/// <param name="Value">Source object</param>
/// <param name="NullValueText">Text to be presented whenever Value argument object is NULL</param>
public ComboBoxNullableItemWrapper(T Value, string NullValueText = "(none)")
{
this._value = Value;
this._nullValueText = NullValueText;
}
/// <summary>
/// Text that will be shown on combobox items
/// </summary>
/// <returns></returns>
public override string ToString()
{
string result;
if (this._value == null)
result = _nullValueText;
else
result = _value.ToString();
return result;
}
}
}
using System.ComponentModel;
namespace ComboBoxWrapperSample
{
public class Person : INotifyPropertyChanged
{
// Declare the event
public event PropertyChangedEventHandler PropertyChanged;
public Person()
{
}
// Name property
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name");
}
}
// Age property
private int _age;
public int Age
{
get { return _age; }
set
{
_age = value;
OnPropertyChanged("Age");
}
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
// Don't forget this override, since it's what defines ao each combo item is shown
public override string ToString()
{
return string.Format("{0} (age {1})", Name, Age);
}
}
}
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
namespace ComboBoxWrapperSample
{
public partial class SampleViewModel : INotifyPropertyChanged
{
// SelectedWrappedItem- This property stores selected wrapped item
public ComboBoxNullableItemWrapper<Person> _SelectedWrappedItem { get; set; }
public ComboBoxNullableItemWrapper<Person> SelectedWrappedItem
{
get { return _SelectedWrappedItem; }
set
{
_SelectedWrappedItem = value;
OnPropertyChanged("SelectedWrappedItem");
}
}
// ListOfPersons - Collection to be injected into ComboBox.ItemsSource property
public ObservableCollection<ComboBoxNullableItemWrapper<Person>> ListOfPersons { get; set; }
public SampleViewModel()
{
// Setup a regular items collection
var person1 = new Person() { Name = "Foo", Age = 31 };
var person2 = new Person() { Name = "Bar", Age = 42 };
List<Person> RegularList = new List<Person>();
RegularList.Add(person1);
RegularList.Add(person2);
// Convert regular collection into a wrapped collection
ListOfPersons = new ObservableCollection<ComboBoxNullableItemWrapper<Person>>();
ListOfPersons.Add(new ComboBoxNullableItemWrapper<Person>(null));
RegularList.ForEach(x => ListOfPersons.Add(new ComboBoxNullableItemWrapper<Person>(x)));
// Set UserSelectedItem so it targes null item
this.SelectedWrappedItem = ListOfPersons.Single(x => x.Value ==null);
}
// INotifyPropertyChanged related stuff
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
}
<Window x:Class="ComboBoxWrapperSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ComboBoxWrapperSample"
xmlns:vm="clr-namespace:ComboBoxWrapperSample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ignore="http://www.ignore.com"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance {x:Type vm:SampleViewModel}, IsDesignTimeCreatable=False}"
Title="MainWindow" Height="200" Width="300">
<StackPanel Orientation="Vertical" Margin="10">
<TextBlock Margin="0,10,0,0">Favorite teacher</TextBlock>
<ComboBox ItemsSource="{Binding ListOfPersons}"
SelectedItem="{Binding SelectedWrappedItem, Mode=TwoWay}">
</ComboBox>
<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<TextBlock>Selected wrapped value:</TextBlock>
<TextBlock Text="{Binding SelectedWrappedItem }" Margin="5,0,0,0" FontWeight="Bold"/>
</StackPanel>
</StackPanel>
</Window>
var enumValues = new ArrayList(Enum.GetValues(typeof(MyEnum)));
enumValues.Insert(0, DBNull.Value);
return enumValues;
public class ComboBoxClearBehavior : Behavior<ComboBox>
{
private Button _addedButton;
private ContentPresenter _presenter;
private Thickness _originalPresenterMargins;
protected override void OnAttached()
{
// Attach to the Loaded event. The visual tree at this point is not available until its loaded.
AssociatedObject.Loaded += AssociatedObject_Loaded;
// If the user or code changes the selection, re-evaluate if we should show the clear button
AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged;
base.OnAttached();
}
protected override void OnDetaching()
{
// Its likely that this is already de-referenced, but just in case the visual was never loaded, we will remove the handler anyways.
AssociatedObject.Loaded -= AssociatedObject_Loaded;
base.OnDetaching();
}
private void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
EvaluateDisplay();
}
/// <summary>
/// Checks to see if the UI should show a Clear button or not based on what is or isn't selected.
/// </summary>
private void EvaluateDisplay()
{
if (_addedButton == null) return;
_addedButton.Visibility = AssociatedObject.SelectedIndex == -1 ? Visibility.Collapsed : Visibility.Visible;
// To prevent the text or content from being overlapped by the button, adjust the margins if we have reference to the presenter.
if (_presenter != null)
{
_presenter.Margin = new Thickness(
_originalPresenterMargins.Left,
_originalPresenterMargins.Top,
_addedButton.Visibility == Visibility.Visible ? ClearButtonSize + 6 : _originalPresenterMargins.Right,
_originalPresenterMargins.Bottom);
}
}
private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
{
// After we have loaded, we will have access to the Children objects. We don't want this running again.
AssociatedObject.Loaded -= AssociatedObject_Loaded;
// The ComboBox primary Grid is named MainGrid. We need this to inject the button control. If missing, you may be using a custom control.
if (!(AssociatedObject.FindChild("MainGrid") is Grid grid)) return;
// Find the content presenter. We need this to adjust the margins if the Clear icon is present.
_presenter = grid.FindChildren<ContentPresenter>().FirstOrDefault();
if (_presenter != null) _originalPresenterMargins = _presenter.Margin;
// Create the new button to put in the view
_addedButton = new Button
{
Height = ClearButtonSize,
Width = ClearButtonSize,
HorizontalAlignment = HorizontalAlignment.Right
};
// Find the resource for the button - In this case, our NoChromeButton Style has no button edges or chrome
if (Application.Current.TryFindResource("NoChromeButton") is Style style)
{
_addedButton.Style = style;
}
// Find the resource you want to put in the button content
if (Application.Current.TryFindResource("RemoveIcon") is FrameworkElement content)
{
_addedButton.Content = content;
}
// Hook into the Click Event to handle clearing
_addedButton.Click += ClearSelectionButtonClick;
// Evaluate if we should display. If there is nothing selected, don't show.
EvaluateDisplay();
// Add the button to the grid - First Column as it will be right justified.
grid.Children.Add(_addedButton);
}
private void ClearSelectionButtonClick(object sender, RoutedEventArgs e)
{
// Sets the selected index to -1 which will set the selected item to null.
AssociatedObject.SelectedIndex = -1;
}
/// <summary>
/// The Button Width and Height. This can be changed in the Xaml if a different size visual is desired.
/// </summary>
public int ClearButtonSize { get; set; } = 15;
}
<ComboBox
ItemsSource="{Binding SomeItemsSource, Mode=OneWay}"
SelectedValue="{Binding SomeId, Mode=TwoWay}"
SelectedValuePath="SomeId">
<i:Interaction.Behaviors>
<behaviors:ComboBoxClearBehavior />
</i:Interaction.Behaviors>
</ComboBox>
<Style x:Key="NoChromeButton"
TargetType="{x:Type Button}">
<Setter Property="Background"
Value="Transparent" />
<Setter Property="BorderThickness"
Value="1" />
<Setter Property="Foreground"
Value="{DynamicResource WindowText}" />
<Setter Property="HorizontalContentAlignment"
Value="Center" />
<Setter Property="VerticalContentAlignment"
Value="Center" />
<Setter Property="Cursor"
Value="Hand"/>
<Setter Property="Padding"
Value="1" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid x:Name="Chrome"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled"
Value="false">
<Setter Property="Foreground"
Value="#ADADAD" />
<Setter Property="Opacity"
TargetName="Chrome"
Value="0.5" />
</Trigger>
<Trigger
Property="IsMouseOver"
Value="True">
<Setter
TargetName="Chrome"
Property="Background"
Value="{DynamicResource ButtonBackgroundHover}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Viewbox x:Key="RemoveIcon"
x:Shared="False"
Stretch="Uniform">
<Canvas Width="58"
Height="58">
<Path Fill="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Control, Mode=FindAncestor}}">
<Path.Data>
<PathGeometry Figures="M 29 0 C 13 0 0 13 0 29 0 45 13 58 29 58 45 58 58 45 58 29 58 13 45 0 29 0 Z M 43.4 40.6 40.6 43.4 29 31.8 17.4 43.4 14.6 40.6 26.2 29 14.6 17.4 17.4 14.6 29 26.2 40.6 14.6 43.4 17.4 31.8 29 Z"
FillRule="NonZero" />
</Path.Data>
</Path>
</Canvas>
</Viewbox>