我是WPF和MVVM Light的新手,如果您能帮助我,我将不胜感激:-)
我想知道如何使用MVVM Light实现combobox来做以下事情:
1)在组合框中选择一项
2)根据选择的值,更改GUI中的其他文本字段。
谢谢你的帮助。
罗曼
发布于 2013-01-02 22:34:06
好的:
查看:
<ComboBox ItemsSource="{Binding SourceData}" SelectedItem="{Binding SelectedSourceData,Mode=TwoWay}"/>
<TextBlock Text="{Binding SelectedDataInTextFormat}"/>ViewModel:
public class ViewModel:ViewModelBase
{
public ObservableCollection<Foo> SourceData{get;set;}
public Foo SelectedSourceData
{
get{return _selectedFoo;}
set{_selectedFoo=value;
RaisePropertyChanged("SelectedSourceData");
SelectedDataInTextFormat=Foo.ToString();
}
public string SelectedDataInTextFormat
{
get{return _selectedDataInTextFormat;}
set{_selectedDataInTextFormat=value;
RaisePropertyChanged("SelectedDataInTextFormat");
}
}基本上,为了确保您的视图模型能够从组合框接收更新后的选定项,请确保将SelectedItem绑定设置为Mode=TwoWay。为了确保在视图模型中发生更改时将数据从视图模型推送到视图,请确保为希望在视图中更新的属性调用RaisePropertyChanged帮助器类。
https://stackoverflow.com/questions/14118062
复制相似问题