2009-06-08 74 views
0

你好我正在構建一個帶有數據網格的wpf應用程序, 這個模式是模型視圖視圖模型。wpf工具包數據網格

所有OG我的屏幕中含有一個ContentControl中,我只是向他分配視圖模型,有一個合適的數據模板,

反正,我的問題是使用組合框的列時,數據上下文是呈現實體,我需要它成爲視圖模型。

最好的解決方案是什麼?

回答

0

我發現實現這個的最好的方法是定義一些外部類的,我在網使用所有的查找和使嵌入他們的模板作爲靜態資源

0

我正在使用另一個數據網格,但它可能是相似的。我做的方式是這樣的:

<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}" 

不知道它是否適用於微軟數據網格,但我想這是值得一試。

+0

xceed快車網格? – abmv 2009-06-12 06:41:58

0

這是我如何使用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。

0

我們最後不得不爲每個靜態屬性的類我們的組合框的列表:

(你不能使類本身的靜態否則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>