gpt4 book ai didi

c# - ComboBox 的默认值(设置一次)

转载 作者:行者123 更新时间:2023-11-30 21:09:49 26 4
gpt4 key购买 nike

我在 SO 和网络上阅读了很多东西,但没有找到答案......我得到了一个绑定(bind)到 Collection 的 ComboBox,它是代码隐藏属性的一个属性,如下所示:

<ComboBox ItemsSource="{Binding Path=LocalizationUtil.AvailableLocales}"/>

这有效,但问题是当我的 UI 被加载时,没有选择默认值,我想设置一个值,因为我知道我的集合至少包含字符串“default”。我看到很多使用 SelectedItemSelectedValue 的东西,但这会创建一种绑定(bind),我希望它在开始时只启动一次。我该怎么做?

最佳答案

首先你必须创建一个像这样的枚举,这样你就可以在组合框上显示它:

[Flags]    
public enum Actions
{
[Description("None")]
None = 0,
[Description("Edit")]
Edit = 1,
[Description("Print")]
Imprimir = 2,
}

在此之后,您必须创建一个方法来将 IEnumerable 返回给您的属性,如下所示:

    /// <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));
}

然后您可以在构造函数中或任何您想要的地方定义它:

    MyActions = EnumHelpers.GetEnumList<Actions>(false);

希望对你有帮助。

关于c# - ComboBox 的默认值(设置一次),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8835481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com