如何设置使用MVVM组合框默认文本 [英] How to set default text in the combobox using MVVM
本文介绍了如何设置使用MVVM组合框默认文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我绑定使用MVVM模式在WPF的组合框。我能够绑定字符串组合框的清单,但我不知道如何设置的组合框的默认值。
嗯,我有名字里面有A,B,C和D的列表。现在我想在默认情况下A应该是默认值。
感谢
<窗口x:类=WpfApplication1.MainWindow
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X = http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:视图模型=CLR的命名空间:WpfApplication1.ViewModel
标题=主窗口HEIGHT =350WIDTH = 525>
< Window.DataContext>
<视图模型:NameViewModel>< /视图模型:NameViewModel>
< /Window.DataContext>
<网格和GT;
<组合框高度=23WIDTH =120的ItemsSource ={绑定名称}/>
< /网格和GT;
公共类NameViewModel
{
私人的IList<串GT; _nameList =新的List<串GT;();
公众的IList<串GT;名称{搞定;组; }
公共NameViewModel()
{
名称=中getAllNames();
}
私人的IList<串GT;中getAllNames()
{
&IList的LT;字符串>名称=新的List<串GT;();
names.Add(A);
names.Add(B);
names.Add(C);
names.Add(D);
返回名称;
}
}
解决方案
我想你应该尝试使用列表项。列表项已选定的属性。
I am binding a combobox in the WPF using MVVM pattern. I am able to bind a list of string with the combobox but I don't know how to set a default value in the combobox. Well I have a list of names which has "A","B","C" and "D". Now I want that by default "A" should come as default value.
Thanks
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ViewModel="clr-namespace:WpfApplication1.ViewModel"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<ViewModel:NameViewModel></ViewModel:NameViewModel>
</Window.DataContext>
<Grid>
<ComboBox Height="23" Width="120" ItemsSource="{Binding Names}"/>
</Grid>
public class NameViewModel
{
private IList<string> _nameList = new List<string>();
public IList<string> Names { get; set; }
public NameViewModel()
{
Names = GetAllNames();
}
private IList<string> GetAllNames()
{
IList<string> names = new List<string>();
names.Add("A");
names.Add("B");
names.Add("C");
names.Add("D");
return names;
}
}
解决方案
I think you should try to use ListItem. ListItem has Selected property
这篇关于如何设置使用MVVM组合框默认文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文