如何使用MVVM在combobox中设置默认文本
我使用MVVM模式绑定WPF中的combobox。 我能够使用combobox绑定字符串列表,但我不知道如何在combobox中设置默认值。 好吧,我有一个免费精选名字大全列表,其中包含“A”,“B”,“C”和“D”。 现在我希望默认情况下“A”应该作为默认值。
谢谢
public class NameViewModel { private IList _nameList = new List(); public IList Names { get; set; } public NameViewModel() { Names = GetAllNames(); } private IList GetAllNames() { IList names = new List(); names.Add("A"); names.Add("B"); names.Add("C"); names.Add("D"); return names; } }
我认为你应该尝试使用ListItem。 ListItem具有Selected属性
我想说实现这个的最简单方法是绑定所选项目……
上述就是C#学习教程:如何使用MVVM在combobox中设置默认文本分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
public class NameViewModel { private IList _nameList = new List (); public IList Names { get; set; } public string SelectedName { get; set; } public NameViewModel() { Names = GetAllNames(); SelectedName = "A"; } private IList GetAllNames() { IList names = new List (); names.Add("A"); names.Add("B"); names.Add("C"); names.Add("D"); return names; } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/936212.html