当WPF ComboBox的选择为空时,它可以显示备用文本吗? [英] Can a WPF ComboBox display alternative text when its selection is null?

查看:653
本文介绍了当WPF ComboBox的选择为空时,它可以显示备用文本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

G'day!



我想让我的WPF ComboBox 选择 null



视图模型具有预期属性:

  public ThingoSelectionViewModel:INotifyPropertyChanged {
public ThingoSelectionViewModel(IProvideThingos){
this.Thingos = IProvideThingos.GetThingos();
}

public ObservableCollection< Thingo> Thingos {get;组; }

public Thingo SelectedThingo {
get {return this.selectedThingo; }
set {//设置this.selectedThingo并引发属性更改通知
}

// ...

}

该视图以预期的方式将XAML绑定到视图模型:

 < ComboBox x:Name =ComboboxDriveSelectedItem ={Binding Path = SelectedThingo}
IsEditable =falseHorizo​​ntalAlignment =LeftMinWidth = 100
IsReadOnly =falseStyle ={StaticResource ComboboxStyle}
Grid.Column =1Grid.Row =1Margin =5SelectedIndex =0>
< ComboBox.ItemsSource>
< CompositeCollection>
< ComboBoxItem IsEnabled =False>选择一件事< / ComboBoxItem>
< CollectionContainer
Collection ={Binding Source = {StaticResource Thingos}}/>
< / CompositeCollection>
< /ComboBox.ItemsSource>
< / ComboBox>

嵌入到顶部的 ComboBoxItem 方式在顶部获得额外的项目。它是纯chrome:视图模型保持纯粹和简单。只有一个问题:用户想要选择一个事情显示每当ComboBox的选择为null。



用户不是默认选择的东西。他们想看到一条消息,告诉他们选择一件事。



我想避免使用 ThingoWrapper 类来污染视图模型> ToString 方法返回Select a thingo如果 .ActualThingo 属性为null,则包装每个 Thingo ,因为我填充 Thingos ,并找出一些方法,以防止用户选择nulled Thingo 。 / p>

有没有办法在使用纯XAML或纯XAML的 ComboBox 边界中显示选择一个事物和视图的代码隐藏类中的几行代码?

解决方案

我发现最小阻力的路径是使用空对象模式有关在.NET Framework中使用此模式的示例,请考虑静态值Double.NaN如果您创建Null对象你的Thingo,在你的视图模型中,你可以将它附加到列表的前面,表示没有选择。为Thingo类创建一个DataTemplate,该类有一个显示选择值的Null Object实例的DataTrigger。



我可以给一个代码示例,但它已经超过了我的床时间。


G'day!

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
    }

    // ...

}

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>

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.

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.

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?

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆