显示“选择项目”当selectedItem为null时在Combobox中 [英] Display "Select an item" in Combobox when selectedItem is null

查看:732
本文介绍了显示“选择项目”当selectedItem为null时在Combobox中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF Combobox绑定到一个viewModel对象的列表。最初SelectedItem为null,因此组合框显示为空。

I have a WPF Combobox that is bound to a list of viewModel objects. Initially the SelectedItem is null, and thus the Combobox display is blank.

当所选项目为null时,我希望Combobox显示选择项目以指导用户从组合框中选择某项。类似的方式,一些文本框包含灰色文本,如输入用户名

When the selected item is null, I would prefer the Combobox to display "Select an item" to guide the user to select something from the combobox. sort of like the way, some text boxes contain gray text such as "enter user name"

有关如何做的任何想法?

Any ideas on how to do this?

编辑:

我最后使用建议覆盖文本框,并根据SelecteItem的值更改其可见性

推荐答案

尝试这个 - 根据你的代码更改ItemsSource和SelectedValue如何实现这个..

Try This- Change ItemsSource and SelectedValue according to your code.I just Showed How you can Achieve this..

<ComboBox Height="23" Name="comboBox1" Width="120" ItemsSource="{Binding OCString,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType=Window}}" SelectedValue="{Binding Selected,Mode=TwoWay,RelativeSource={RelativeSource AncestorType=Window},UpdateSourceTrigger=PropertyChanged}">
        <ComboBox.Style>
            <Style TargetType="{x:Type ComboBox}">
                <Style.Triggers>
                    <Trigger Property="SelectedIndex" Value="-1">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <ComboBox Text="Select an Item" IsReadOnly="True" IsEditable="True" ItemsSource="{Binding OCString,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType=Window}}" SelectedValue="{Binding Selected,Mode=TwoWay,RelativeSource={RelativeSource AncestorType=Window},UpdateSourceTrigger=PropertyChanged}"/>

    </ControlTemplate>
    </Setter.Value>
    </Setter>
                    </Trigger>
                </Style.Triggers>
    </Style>
    </ComboBox.Style>
    </ComboBox>

或Simply - >

or Simply->

<ComboBox Text="Select an Item" IsReadOnly="True" IsEditable="True" ItemsSource="{Binding OCString,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType=Window}}" SelectedValue="{Binding Selected,Mode=TwoWay,RelativeSource={RelativeSource AncestorType=Window},UpdateSourceTrigger=PropertyChanged}"/>

这篇关于显示“选择项目”当selectedItem为null时在Combobox中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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