当 WPF ComboBox 的选择为空时,它可以显示替代文本吗? [英] Can a WPF ComboBox display alternative text when its selection is null?
问题描述
今天!
我希望我的 WPF ComboBox
在其数据绑定选择为 null
时显示一些替代文本.
I want my WPF ComboBox
to display some alternative text when its data-bound selection is null
.
视图模型具有预期的属性:
The view model has the expected properties:
public ThingoSelectionViewModel : INotifyPropertyChanged {
public ThingoSelectionViewModel(IProvideThingos) {
this.Thingos = IProvideThingos.GetThingos();
}
public ObservableCollection<Thingo> Thingos { get; set; }
public Thingo SelectedThingo {
get { return this.selectedThingo; }
set { // set this.selectedThingo and raise the property change notification
}
// ...
}
该视图以预期的方式将 XAML 绑定到视图模型:
The view has XAML binding to the view model in the expected way:
<ComboBox x:Name="ComboboxDrive" SelectedItem="{Binding Path=SelectedThingo}"
IsEditable="false" HorizontalAlignment="Left" MinWidth="100"
IsReadOnly="false" Style="{StaticResource ComboboxStyle}"
Grid.Column="1" Grid.Row="1" Margin="5" SelectedIndex="0">
<ComboBox.ItemsSource>
<CompositeCollection>
<ComboBoxItem IsEnabled="False">Select a thingo</ComboBoxItem>
<CollectionContainer
Collection="{Binding Source={StaticResource Thingos}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
楔入顶部的 ComboBoxItem
是一种在顶部获得额外项目的方法.它是纯 chrome:视图模型保持纯粹和简单.只有一个问题:用户希望在 ComboBox 的选择为空时显示选择事物".
The ComboBoxItem
wedged into the top is a way to get an extra item at the top. It's pure chrome: the view model stays pure and simple. There's just one problem: the users want "Select a thingo" displayed whenever the ComboBox' selection is null.
用户不想要默认选择的东西.他们希望看到一条消息,告诉他们选择一个事物.
The users do not want a thingo selected by default. They want to see a message telling them to select a thingo.
我想避免使用 ThingoWrapper
类和 ToString
方法污染视图模型,如果它的 .ActualThingo
属性为空,在我填充 Thingos
时包装每个 Thingo
,并找出一些方法来防止用户选择空的 Thingo
.
I'd like to avoid having to pollute the viewmodel with a ThingoWrapper
class with a ToString
method returning "Select a thingo" if its .ActualThingo
property is null, wrapping each Thingo
as I populate Thingos
, and figuring out some way to prevent the user from selecting the nulled Thingo
.
有没有办法使用纯 XAML 或纯 XAML 和视图的代码隐藏类中的几行代码在 ComboBox
的边界内显示选择事物"?
Is there a way to display "Select a thingo" within the ComboBox
' boundaries using pure XAML, or pure XAML and a few lines of code in the view's code-behind class?
推荐答案
我在这里找到的阻力最小的路径是使用 Null Object Pattern 对于在 .NET Framework 中使用此模式的示例,如果您为 Thingo 创建 Null Object,请考虑静态值 Double.NaN,在您的视图模型中可以将其附加到列表的前面以表示未选择任何内容".为 Thingo 类创建一个 DataTemplate,该类具有一个用于显示选择值"的 Null 对象实例的 DataTrigger.
The path of least resistance here that I've found is to use the Null Object Pattern For an example of using this pattern in the .NET Framework, consider the static value Double.NaN if you create a Null Object for your Thingo, in your view model you can append it to the front of your list to signify "nothing is selected". Create a DataTemplate for the Thingo class that has a DataTrigger for the Null Object instance that shows "Select a Value".
我可以提供一个代码示例,但它已经过了我的睡觉时间.
I could give a code sample but it's past my bed time.
这篇关于当 WPF ComboBox 的选择为空时,它可以显示替代文本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!