DataGridComboBoxColumn的Databind项目源到视图模型中的集合不起作用

时间:2015-06-24 14:28:40

标签: c# wpf xaml datagrid datagridcomboboxcolumn

我在这里要做的是将DataGridComboBoxColumn的Itemsource数据绑定到声明为项目视图模型属性的字符串集合。 Datagrid本身绑定到另一个viewmodel,该viewmodel有一个viewModel集合,表示datagrid上的行。 我所有其他绑定都能正常工作。该系列也已填满,但组合框仍为空。

XAML:

<Window.Resources>
    <ResourceDictionary>
        <local:GeneralDataGridViewModel x:Key="generalDataGridVm"/>
    </ResourceDictionary>
</Window.Resources>
<Grid>
      <DataGrid DataContext="{StaticResource generalDataGridVm}"
                  ItemsSource="{Binding Collection}">
     <DataGrid.Columns>

            <DataGridComboBoxColumn x:Name="chbCodes"
                                 Header="Code"                  
                                 ItemsSource="{Binding Path=DataContext.Collection, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
     </DataGrid.Columns>
    </DataGrid>
</Grid>

C#ItemViewModel:

public class ItemViewModel : INotifyPropertyChanged
    {

        private ObservableCollection<string> _collection;


        public ObservableCollection<string> Collection
        {
            get
            {
                return _collection;
            }
        }
        public Model Model { get; set; }


        public string Code
        {
            get { return Model.Code; }
            set { Model.Code = value; }
        }

        public ItemViewModel()
        {

        }



        public ItemViewModel(Model model)
        {
            Model = model;
            _collection = new ObservableCollection<string>();
            _collection.Add(model.Code);
            Model.PropertyChanged += Model_PropertyChanged;
        }


        public void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
        }

    }

c#DataGridViewModel:

public class GeneralDataGridViewModel
{
    private ObservableCollection<ItemViewModel> _collection;
    public ObservableCollection<ItemViewModel> Collection
    {
        get { return _collection; }
        set
        {
            _collection = value;
            NotifyPropertyChanged("Collection");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    public GeneralDataGridViewModel()
        : base()
    {
        _collection = new ObservableCollection<ItemViewModel>();
    }

    public GeneralDataGridViewModel(List<Model> models)
    {
        _collection = new ObservableCollection<ItemViewModel>((from m in models
                                                               select new ItemViewModel(m)).ToList());
    }
}

C#型号:

public class Model: INotifyPropertyChanged
    {
        public string Code { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(String property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
        public override string ToString()
        {
            return Code;
        }

    }

1 个答案:

答案 0 :(得分:0)

您发布的代码无法编译,但查看该问题可能与数据上下文有关,您将其设置为静态资源。如果要执行此操作,视图模型必须位于资源字典中。 XAML中的绑定很好,请参阅下面的示例:

XAML:

<Window.Resources>
    <ResourceDictionary>
        <local:GeneralDataGridViewModel x:Key="generalDataGridVm"/>
    </ResourceDictionary>
</Window.Resources>
<StackPanel Orientation="Vertical">
    <DataGrid DataContext="{StaticResource generalDataGridVm}" Name="DataGrid1" ItemsSource="{Binding Collection}">
        <DataGrid.Columns>
            <DataGridComboBoxColumn x:Name="chbCodes"
                         Header="Code"                  
                         ItemsSource="{Binding Path=DataContext.Collection, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
        </DataGrid.Columns>
    </DataGrid>
</StackPanel>

通过在XAML中声明generalDataGridVm,在XAML中调用构造函数,让我们假设构造提供了集合的值:

public GeneralDataGridViewModel() : base()
{
    _collection = new ObservableCollection<ItemViewModel>();

    _collection.Add(new ItemViewModel(new Model("code1")));
    _collection.Add(new ItemViewModel(new Model("code2")));
    _collection.Add(new ItemViewModel(new Model("code3")));
    _collection.Add(new ItemViewModel(new Model("code4")));
}

使用此代码,它会生成一个填充的列表:

enter image description here

所以我认为你只需要确保你正确地声明你的视图模型(我建议不要在XAML中创建它,除非有一个很好的理由)。

然后确保在视图模型的特定实例中正确更新集合。