如何使用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属性
我想说实现这个的最简单方法是绑定所选项目……
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; } }
- 尝试使用MySQL Entity Framework 6运行控制台应用程序时出现ConfigurationErrorException
- 为什么我的Azure WebJobs“settings.job”文件被忽略?
- 在构建期间设置web.config属性(而不是通过发布)
- 从字符串数组的列表转换为对象列表
- 关闭文件而不使用
- Selenium:Firefox驱动程序,使用c#中的SelectElement从下拉列表中选择项目无法正常工作
- 如何使用C#使用Protobuf进行非阻塞套接字读取?
- asp.net MVC – 如何通过不同的存储库类共享同一个SqlConnection实例
- unit testing自定义ConfigurationElement和ConfigurationElementCollection
- 为什么MarkdownSharp不能编码我的HTML?
- 如何正确使用javascript反序列化将json字符串转换为复杂对象?