你好我正在构建一个带有数据网格的wpf应用程序, 这个模式是模型视图视图模型。wpf工具包数据网格
所有OG我的屏幕中含有一个ContentControl中,我只是向他分配视图模型,有一个合适的数据模板,
反正,我的问题是使用组合框的列时,数据上下文是呈现实体,我需要它成为视图模型。
最好的解决方案是什么?
你好我正在构建一个带有数据网格的wpf应用程序, 这个模式是模型视图视图模型。wpf工具包数据网格
所有OG我的屏幕中含有一个ContentControl中,我只是向他分配视图模型,有一个合适的数据模板,
反正,我的问题是使用组合框的列时,数据上下文是呈现实体,我需要它成为视图模型。
最好的解决方案是什么?
我发现实现这个的最好的方法是定义一些外部类的,我在网使用所有的查找和使嵌入他们的模板作为静态资源
我正在使用另一个数据网格,但它可能是相似的。我做的方式是这样的:
<ObjectDataProvider x:Key="VM" ObjectInstance="{x:Null}" x:Name="vm"/>
然后分配DataContext的(无论是构造或DataContextChanged仅事件)之后,我这样做:
在XAML,我在资源定义的ObjectDataProvider的:
(this.Resources["VM"] as ObjectDataProvider).ObjectInstance = this.DataContext;
在ComboBox XAML,我使用的作为结合源:
ItemsSource="{Binding Source={StaticResource VM}, Path=SomeItems, Mode=OneWay}"
不知道它是否适用于微软数据网格,但我想这是值得一试。
这是我如何使用ViewModel与ComboBoxes,DataContext是ViewModel,而不是底层实体(列表<人>)。
视图模型(人是有名字和年龄的简单类):
public class PeopleViewModel : INotifyPropertyChanged
{
private List<Person> _peopleList;
private Person _selectedPerson;
public PeopleViewModel()
{
// initialize with sample data
_peopleList = getPeopleList();
}
// gets sample data
private List<Person> getPeopleList()
{
var result = new List<Person>();
for (int i = 0; i < 10; i++)
{
result.Add(new Person("person " + i, i));
}
return result;
}
public List<Person> PeopleList
{
get { return _peopleList; }
}
public Person SelectedPerson
{
get { return _selectedPerson; }
set
{
if (_selectedPerson == value) return;
_selectedPerson = value;
// required so that View know about changes
OnPropertyChanged("SelectedPerson");
}
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
// WPF will listen on this event for changes
public event PropertyChangedEventHandler PropertyChanged;
}
的XAML组合框:
<ComboBox Name="cmbEnum" Width="150" ItemsSource="{Binding Path=PeopleList}" SelectedValue="{Binding Path=SelectedPerson}" SelectedValuePath="" DisplayMemberPath="Name" ></ComboBox>
而在后面的代码我可以这样做:
public Window2()
{
InitializeComponent();
vm = new PeopleViewModel();
// we are listening on changes of ViewModel, not ComboBox
vm.PropertyChanged += new PropertyChangedEventHandler(vm_PropertyChanged);
this.DataContext = vm;
}
void vm_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "SelectedPerson")
{
MessageBox.Show(vm.SelectedPerson.Age.ToString());
}
}
// button1_Click should be probably replaced by Command
private void button1_Click(object sender, RoutedEventArgs e)
{
// sample showing that GUI is updated when ViewModel changes
vm.SelectedPerson = vm.PeopleList[2];
}
希望这有帮助,我对WPF很陌生,如果这是使用MVVM的正确方式,我想听听任何反馈,我认为它很优雅因此,您只能在代码中处理ViewModel和Model,并且可以替换View。
我们最后不得不为每个静态属性的类我们的组合框的列表:
(你不能使类本身的静态否则XAML将无法打开它,但你不会得到编译错误)
例如:
public class ZoneList
{
private static readonly IList<Zone> _Items = new List<Zone>();
public static IList<Zone> Items
{
get { return _Items; }
}
}
,然后在XAML:
<UserControl.Resources>
<ResourceDictionary>
<ObjectDataProvider x:Key="myZoneList" ObjectType="{x:Type StaticLists:ZoneList}"/>
</ResourceDictionary>
</UserControl.Resources>
<ComboBox ItemsSource="{Binding Path=Items, Source={StaticResource myZoneList}}"></ComboBox>
xceed快车网格? – abmv 2009-06-12 06:41:58