C# 组合框的默认值(设置一次)
我在网上和网上读了很多东西,但没有找到答案。。。 我得到了一个绑定到集合的组合框,该集合是代码隐藏属性的属性,如下所示:C# 组合框的默认值(设置一次),c#,wpf,xaml,C#,Wpf,Xaml,我在网上和网上读了很多东西,但没有找到答案。。。 我得到了一个绑定到集合的组合框,该集合是代码隐藏属性的属性,如下所示: <ComboBox ItemsSource="{Binding Path=LocalizationUtil.AvailableLocales}"/> /// <summary> /// Get the list with names and descriptions of Enum /// </summary>
<ComboBox ItemsSource="{Binding Path=LocalizationUtil.AvailableLocales}"/>
/// <summary>
/// Get the list with names and descriptions of Enum
/// </summary>
/// <typeparam name="T">Enum Type</typeparam>
/// <param name="usarNome">if true the key is the Enum name</param>
/// <returns>List with names and descriptions</returns>
public static IEnumerable<KeyValuePair<string, T>> GetEnumList<T>(bool usarNome)
{
var x = typeof(T).GetFields().Where(info => info.FieldType.Equals(typeof(T)));
return from field in x
select new KeyValuePair<string, T>(GetEnumDescription(field, usarNome), (T)Enum.Parse(typeof(T), field.Name, false));
}
这是可行的,但问题是当我的UI加载时,没有选择默认值,我想设置一个值,因为我知道我的集合至少包含字符串“default”。
我在使用SelectedItem
或SelectedValue
时看到了很多东西,但这会创建一种绑定,我希望它只在开始时启动一次。
我该怎么做?
<ComboBox ItemsSource="{Binding Path=LocalizationUtil.AvailableLocales}" SelectedIndex="0"/>
首先,您必须创建一个类似于此的枚举,以便能够在combobox上显示它:
[Flags]
public enum Actions
{
[Description("None")]
None = 0,
[Description("Edit")]
Edit = 1,
[Description("Print")]
Imprimir = 2,
}
在此之后,必须创建一个方法将IEnumerable返回到属性,如下所示:
<ComboBox ItemsSource="{Binding Path=LocalizationUtil.AvailableLocales}"/>
/// <summary>
/// Get the list with names and descriptions of Enum
/// </summary>
/// <typeparam name="T">Enum Type</typeparam>
/// <param name="usarNome">if true the key is the Enum name</param>
/// <returns>List with names and descriptions</returns>
public static IEnumerable<KeyValuePair<string, T>> GetEnumList<T>(bool usarNome)
{
var x = typeof(T).GetFields().Where(info => info.FieldType.Equals(typeof(T)));
return from field in x
select new KeyValuePair<string, T>(GetEnumDescription(field, usarNome), (T)Enum.Parse(typeof(T), field.Name, false));
}
//
///获取包含枚举名称和描述的列表
///
///枚举类型
///如果为true,则键为枚举名称
///列出名称和描述
公共静态IEnumerable GetEnumList(bool-UsarName)
{
var x=typeof(T).GetFields(),其中(info=>info.FieldType.Equals(typeof(T));
从x中的字段返回
选择新的KeyValuePair(GetEnumDescription(field,usarName),(T)Enum.Parse(typeof(T),field.Name,false));
}
然后您可以在构造函数中或任何您想要的地方定义它:
MyActions = EnumHelpers.GetEnumList<Actions>(false);
MyActions=EnumHelpers.GetEnumList(false);
希望它对您有所帮助。首先,您必须创建一个类似于此的枚举,以便能够在组合框上显示它:
[Flags]
public enum Actions
{
[Description("None")]
None = 0,
[Description("Edit")]
Edit = 1,
[Description("Print")]
Imprimir = 2,
}
在此之后,必须创建一个方法将IEnumerable返回到属性,如下所示:
<ComboBox ItemsSource="{Binding Path=LocalizationUtil.AvailableLocales}"/>
/// <summary>
/// Get the list with names and descriptions of Enum
/// </summary>
/// <typeparam name="T">Enum Type</typeparam>
/// <param name="usarNome">if true the key is the Enum name</param>
/// <returns>List with names and descriptions</returns>
public static IEnumerable<KeyValuePair<string, T>> GetEnumList<T>(bool usarNome)
{
var x = typeof(T).GetFields().Where(info => info.FieldType.Equals(typeof(T)));
return from field in x
select new KeyValuePair<string, T>(GetEnumDescription(field, usarNome), (T)Enum.Parse(typeof(T), field.Name, false));
}
//
///获取包含枚举名称和描述的列表
///
///枚举类型
///如果为true,则键为枚举名称
///列出名称和描述
公共静态IEnumerable GetEnumList(bool-UsarName)
{
var x=typeof(T).GetFields(),其中(info=>info.FieldType.Equals(typeof(T));
从x中的字段返回
选择新的KeyValuePair(GetEnumDescription(field,usarName),(T)Enum.Parse(typeof(T),field.Name,false));
}
然后您可以在构造函数中或任何您想要的地方定义它:
MyActions = EnumHelpers.GetEnumList<Actions>(false);
MyActions=EnumHelpers.GetEnumList(false);
希望它能对您有所帮助。这可能的重复不适用于我的收藏:/n您是否将组合框的SelectedValue绑定到任何东西上?不,我这样做了,但我真的不喜欢,因为SelectedValue随后被绑定。我想要的是在加载UI时只执行一次的内容可能重复的内容不适用于我拥有的集合:/n您是否将组合框的SelectedValue绑定到任何内容?不,我这样做了,但我真的不喜欢,因为SelectedValue随后被绑定。我想要的是在加载UI时只执行一次。我尝试了它,但我不能绝对确定“default”字符串是否是列表中的第一个:/实际上是,但在最终版本中我无法控制它!我试过了,但我不能绝对肯定“default”字符串是否会在列表中的第一个:/实际上是,但在最终版本中我无法控制它!如果你需要的话,我也可以发布我是如何在XAML上写的。我得到了这个,但这让我有点惊讶,你不能轻易地为组合框设置默认值,唯一的解决方案就是定义这么多东西:哦,我在寻找一个“神奇”的XAML属性,所以我会坚持随机用户的解决方案!如果你需要的话,我也可以发布我是如何在XAML上写的。我得到了这个,但这让我有点惊讶,你不能轻易地为组合框设置默认值,唯一的解决方案就是定义这么多东西:哦,我在寻找一个“神奇”的XAML属性,所以我会坚持随机用户的解决方案!