wpf工具包数据网格

时间:2009-06-08 20:48:38

标签: wpf wpf-controls wpftoolkit

你好我正在用数据网格构建一个wpf应用程序, 模式是模型视图模型。

所有og我的屏幕都包含一个内容控件,我只是为他分配了一个具有合适数据模板的视图模型,

无论如何,我的问题是组合框列,数据上下文是呈现的实体,我需要它作为视图模型。

最好的解决方案是什么?

4 个答案:

答案 0 :(得分:0)

我正在使用另一个数据网格,但它可能类似。我这样做的方式是这样的:

在XAML中,我在资源中定义了一个ObjectDataProvider:

<ObjectDataProvider x:Key="VM" ObjectInstance="{x:Null}" x:Name="vm"/>

然后在分配DataContext(构造函数或DataContextChanged事件)后,我这样做了:

(this.Resources["VM"] as ObjectDataProvider).ObjectInstance = this.DataContext;

在Combobox xaml中,我使用它作为绑定源:

ItemsSource="{Binding Source={StaticResource VM}, Path=SomeItems, Mode=OneWay}"

不确定它是否适用于microsoft datagrid,但我想这值得一试。

答案 1 :(得分:0)

这就是我使用ViewModel和ComboBoxes的方式,DataContext是ViewModel,而不是底层实体(List&lt; Person&gt;)。

ViewModel(Person是一个名字和年龄的简单类):

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

ComboBox的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,并且可以替换视图。

答案 2 :(得分:0)

我发现实现这一点的最佳方法是为我在网格中使用的所有查找定义一些外部类,并将它们作为静态资源嵌入模板中

答案 3 :(得分: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>
相关问题