comboBox的默认值 [英] Default value for comboBox

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

问题描述

您好,我尝试为comboBox设置默认值。

Hi I try set default value for comboBox.

XAML:

        <ComboBox Name="StatusCombo"
                  Style="{StaticResource statusComboStyle}"
                  SelectedValuePath="StatusMsg" 
                  SelectedValue="{Binding Path=SelectedStatus}"/>

我使用caliburn.Micro。我将 List< string,StatusItem> 绑定到ComboBox,它工作得很好。

I use caliburn.Micro. I bind List<string, StatusItem> to ComboBox, it works good.

状态项是简单类是:

public class StatusItem
{
    public string StatusMsg { get; set; }
    public BitmapImage StatusImg { get; set; }
}

App.xaml

在app.xaml中定义空字符串

I define empty string in app.xaml

<System:String x:Key="empty"></System:String>

statusComboStyle在这里:

statusComboStyle is here:

   <Style x:Key="statusComboStyle" TargetType="{x:Type ComboBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path= SelectedStatus}" Value="{StaticResource empty}">
                   <Setter Property="SelectedIndex" Value="0"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

但是一定是错误的,因为我得到这个编译错误:

But something must be wrong, because I get this compile error:

{在类型System.String上找不到匹配的构造函数,您可以使用Arguments或FactoryMethod指令来构造此类型。}

at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at Spirit.Views.LogOnView.InitializeComponent() in c:\Users\Jan\Documents\Visual Studio 2010\Projects\C#\Pokec_Messenger\ver.beta\Pokec__Messenger\Spirit_Caliburn_Micro_v1.0\Views\LogOnView.xaml:line 1
at Spirit.Views.LogOnView..ctor() in C:\Users\Jan\Documents\Visual Studio 2010\Projects\C#\Pokec_Messenger\ver.beta\Pokec__Messenger\Spirit_Caliburn_Micro_v1.0\Views\LogOnView.xaml.cs:line 24

如果SelectedStatus是空字符串,如果值为空,我将检入数据触发器。string我设置组合框中的frist项。

I check in data trigger if SelectedStatus is empty string, if value is empty string I set frist item in comboBox.

推荐答案

你不需要创建自己的空字符串,字符串有一个静态字段,所以你可以设置为这样的样式:

You do not need to create your own empty string, string has a static field for that, so you could set it in the style like this:

Value="{x:Static System:String.Empty}"

为什么你只是在你的风格立即使用正常的setter?

And why don't you just use a normal setter in your style right away?

<Style x:Key="statusComboStyle" TargetType="{x:Type ComboBox}">
    <Setter Property="SelectedIndex" Value="0"/>
</Style>

(您应确保ComboBox中至少有一个项目)

(You should make sure that there is at least one item in the ComboBox)

这篇关于comboBox的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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