以mvvm模式在wpf中绑定数据 [英] Data binding in wpf in mvvm pattern
问题描述
如何使用mvvm模式在组合框中使用wpf进行数据绑定。和数据绑定区域中的初学者
最好的办法是在ViewModel中使用CollectionView
,然后绑定你的ComboBox到CollectionView。 CollectionView有一个CurrentChanged事件,您可以使用它来处理用户选择更改,如下所示(假设您的ComboBox项目存储在List< string>中)
class YourWindowViewModel
{
public CollectionView comboBoxItems {获取; private set ; }
public YourWindowViewModel()
{
// ComboBox的原始列表
IList< string> ComboBoxList = new List< string>();
comboBoxItems = new CollectionView(ComboBoxList);
comboBoxItems.MoveCurrentTo(ComboBoxList [ 0 ]);
comboBoxItems.CurrentChanged + = new EventHandler(comboBox_CurrentChanged);
}
void comboBox_CurrentChanged( object sender,EventArgs e)
{
// 如果选择更改,请在此处工作
}
}
希望这有帮助
我建议使用一个可观察的属性您的Viewmodel:
私人 CollectionView _comboBoxOptions = 空跨度>;
public CollectionView comboBoxOptions
{
get { return _comboBoxOptions; }
set
{
if (_ comboBoxOptions!= value )
{
_comboBoxOptions = value ;
RaisePropertyChanged( comboBoxOptions);
}
}
}
您还必须在Viewmodel中使用INotifyPropertyChanged:< br $> b $ b
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged( string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler!= null )handler( this , new PropertyChangedEventArgs(name));
}
然后在您的XAML中将您的控件绑定到该属性:
< ComboBox >
< Binding 路径 = comboBoxOptions
ValidatesOnDataErrors < span class =code-keyword> = True
< span class =code-attribute>
NotifyOnValidationError = True
< span class =code-attribute> 模式 = OneWay
UpdateSourceTrigger = LostFocus / >
< / ComboBox >
您可以在Viewmodel的构造函数中初始化comboBoxOptions属性。
希望这有帮助
劳伦斯
你好
查看此代码,它可能对您有帮助。
public class 订单
{
private readonly string _instrument;
private readonly double _price ;
private readonly long _quantity ;
public 订单(字符串工具,双价格,长数量)
{
_instrument = instrument;
_price = price;
_quantity =数量;
}
public string 工具
{
get { return _instrument; }
}
public double 价格
{
获取 {返回 _price; }
}
public long 数量
{
get { return _quantity; }
}
}
public void PopulateGrid (网格)
{
BindingList< order> orders = new BindingList< order>();
orders.Add( new 订单( Instrument1, 10 。 55 , 34 跨度>));
orders.Add( new 订单( Instrument2, 12 。 26 , 154 跨度>));
orders.Add( new 订单( Instrument1, 13 。 16 , 14 跨度>));
orders.Add( new 订单( Instrument5, 9 。 85 , 52 跨度>));
orders.Add( new 订单( Instrument1, 16 。 47 , 11 跨度>));
grid.DataSource = orders;
}
< / 订单 > < / 订单 > ;
更多信息可以在这里找到 http://www.dapfor.com/en/net-suite/net-grid/features/hierarchical-data-binding [< a href =http://www.dapfor.com/en/net-suite/net-grid/features/hierarchical-data-binding\"target =_ blanktitle =New Window> ^ ]
how to do data binding in wpf for combobox ia m using mvvm pattern. and beginner in data binding area
Your best bet is to use aCollectionView
in your ViewModel, and bind your ComboBox to the CollectionView. The CollectionView has a CurrentChanged event which you can use to act on user selection changes, something like this (assuming your ComboBox Items are stored in a List<string>)
class YourWindowViewModel { public CollectionView comboBoxItems { get; private set; } public YourWindowViewModel() { //Original list for ComboBox IList<string> ComboBoxList = new List<string>(); comboBoxItems = new CollectionView(ComboBoxList); comboBoxItems.MoveCurrentTo(ComboBoxList[0]); comboBoxItems.CurrentChanged += new EventHandler(comboBox_CurrentChanged); } void comboBox_CurrentChanged(object sender, EventArgs e) { //Do work here if selection changes } }
Hope this helps
I would suggest using an observable property in your Viewmodel:
private CollectionView _comboBoxOptions = null; public CollectionView comboBoxOptions { get { return _comboBoxOptions; } set { if (_comboBoxOptions != value) { _comboBoxOptions = value; RaisePropertyChanged("comboBoxOptions"); } } }
You would also have to use INotifyPropertyChanged in the Viewmodel:
public event PropertyChangedEventHandler PropertyChanged; protected void RaisePropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(name)); }
Then in your XAML bind your control to that property :
<ComboBox> <Binding Path="comboBoxOptions" ValidatesOnDataErrors="True" NotifyOnValidationError="True" Mode="OneWay" UpdateSourceTrigger="LostFocus"/> </ComboBox>
You could initialise the comboBoxOptions property in the constructor for your Viewmodel.
Hope this helps
Laurence
Hello
have a look on this code, it may help you.
public class Order { private readonly string _instrument; private readonly double _price; private readonly long _quantity; public Order(string instrument, double price, long quantity) { _instrument = instrument; _price = price; _quantity = quantity; } public string Instrument { get { return _instrument; } } public double Price { get { return _price; } } public long Quantity { get { return _quantity; } } } public void PopulateGrid(Grid grid) { BindingList<order> orders = new BindingList<order>(); orders.Add(new Order("Instrument1", 10.55, 34)); orders.Add(new Order("Instrument2", 12.26, 154)); orders.Add(new Order("Instrument1", 13.16, 14)); orders.Add(new Order("Instrument5", 9.85, 52)); orders.Add(new Order("Instrument1", 16.47, 11)); grid.DataSource = orders; } </order></order>
more can be found here http://www.dapfor.com/en/net-suite/net-grid/features/hierarchical-data-binding[^]
这篇关于以mvvm模式在wpf中绑定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!