ComboBox的水印行为 [英] Watermark Behavior for ComboBox

查看:53
本文介绍了ComboBox的水印行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个 combobox 绑定到 dictionary 。我想做的是在 combobox 中有一个默认项目,例如请选择一个项目...,当用户实际单击该项目时,该项目就会消失。 comboxbox 选择一个项目。

I currently have a combobox bound to a dictionary. What I'm trying to do is have a default item in the combobox, such as "Please select an item...", which disappears when the user actually clicks on the combobox to select an item.

我在此网站上看到过类似的问题,但似乎无法获得任何适合我的解决方案。我唯一的运气是将其放入我的xaml组合框代码中:

I've seen similar questions on this site, but can't seem to get any of the solutions to work properly for me. The only luck I've had is by putting this in my xaml combobox code:

<IsEditable="True" IsReadOnly="True" Text="Please select an item..."/>

但这当然会改变 comboxbox的外观,但我不希望它看起来可编辑。

But this of course changes the look of the combobox and I don't want it look editable.

我的代码后面:

private Dictionary<string, string> imageTypes = new Dictionary<string, string>();

public MainWindow()
{
    InitializeComponent();
    AddImage_Types();
}

public void AddImage_Types()
{
    imageTypes.Add("*.png", Png);
    imageTypes.Add("*.jpg *.jpeg *jfif", Jpg);
}

public Dictionary<string, string> ImageTypes
{
    get
    {
        return imageTypes;
    }
}

这是组合框的xaml:

And here's my xaml for the combobox:

<ComboBox Name="imageCB"
          ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}, Path=ImageTypes}"
          SelectedValuePath="Value"
          DisplayMemberPath="Key" >
</ComboBox>

我尝试使用触发器和样式以及以下答案:https://stackoverflow.com/a/16782339/2480598

I've tried using triggers and styles as well like this answer: https://stackoverflow.com/a/16782339/2480598

我确定是

注意:默认情况下,我的意思是当加载窗口时,某些文本已经显示在窗口中了。组合框,例如请选择一个项目...。当用户单击组合框从下拉列表中选择一项时,该选项将消失。

Note: By default item, I mean that when the window is loaded, some text is already displayed in the combo box like "Please select an item...". This will go away when the user clicks the combo box to select an item from the dropdown.

推荐答案

您要执行的操作这并不是WPF的工作原理。最好的方法是使用数据验证/绑定,因此用户必须先选择某些内容才能继续前进;否则,如果用户没有选择某项内容,则抛出验证错误(组合框周围的红线)...甚至只是首先在其中提供默认值。

The way you want to do this isn't really how WPF works. The best way to do this would be to use data validation / bindings, so user can't move on until something is selected; or a validation error is thrown (red lines around the combobox) if the user doesn't select something... or even just give a default value there in the first place.

但是,如果您希望它按照您的要求工作,则可以:

But, if you want it to work the way you're asking, you could:

a)在您的帐户中输入请选择一个项目...字典,然后让事件处理程序在用户选择其他内容时将其删除

a) have the "Please select an item..." in your dictionary, then have an event handler remove it when the user selects something else

b)将请选择一个项目...作为绑定中的唯一项目字典,然后让事件处理程序在用户打开组合框时更改绑定

b) have the "Please select an item..." as the only item in your bound dictionary, then have an event handler change the binding when the user opens the combo box

c)在组合框的顶部放置一个带有透明背景的标签,单击该标签时继续消失

c) put a label ontop of the combo box with a transparent background that, when clicked on, disappears

d)(未经测试),如何调整此链接?::

d) (untested), how about adjusting the code from this link?:

<ComboBox IsTextSearchEnabled="True" StaysOpenOnEdit="True" 
          Width="150" Height="24"  IsReadOnly="False" IsEditable="True">
  <ComboBox.Resources>
    <VisualBrush x:Key="HelpBrush" TileMode="None" Opacity="0.4" Stretch="None" AlignmentX="Left">
      <VisualBrush.Visual>
        <TextBlock FontStyle="Italic" Text="Type or select from list"/>
      </VisualBrush.Visual>
    </VisualBrush>
  </ComboBox.Resources>
  <ComboBox.Style>
    <Style TargetType="ComboBox">
      <Style.Triggers>
        <Trigger Property="Text" Value="{x:Null}">
          <Setter Property="Background" Value="{StaticResource HelpBrush}"/>
        </Trigger>
        <Trigger Property="Text" Value="">
          <Setter Property="Background" Value="{StaticResource HelpBrush}"/>
        </Trigger>
      </Style.Triggers>
    </Style>
  </ComboBox.Style>
  <ComboBoxItem>First item</ComboBoxItem>
  <ComboBoxItem>Another item</ComboBoxItem>
</ComboBox>

这篇关于ComboBox的水印行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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