DataGrid 内 ComboBox 上的 ItemSource 绑定 [英] ItemSource Binding on ComboBox inside a DataGrid

查看:63
本文介绍了DataGrid 内 ComboBox 上的 ItemSource 绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DataGrid 内的 ComboBox 未填充列表.我认为 ItemSource Path 存在问题:

The ComboBox inside the DataGrid is not getting populated with the List. I think there is an issue with ItemSource Path :

查看(DataGrid 的 xaml 代码):

View (xaml code for the DataGrid) :

<DataGrid CanUserAddRows="True" ItemsSource="{Binding Path=GridCollection, Mode=TwoWay}" AutoGenerateColumns="False" IsReadOnly="False">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Column 1">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding Path=DataContext.ComboBoxList}" BorderThickness="0" BorderBrush="Transparent" SelectedValue="{Binding Col1, Mode=TwoWay}"/>
                    </ComboBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Header="Column 2" Width="170" Binding="{Binding Col2, Mode=TwoWay}"/>
</DataGrid>

查看模型(我为 ItemModel 创建了一个可观察的集合,当我更新文本列的值并将值分配给模型对象时,这工作正常):

View Model (I've created an observable collection for ItemModel, this is working fine when I update the values for Text Column and values are assigned to the Model object) :

public ObservableCollection<ItemModel> GridCollection
{
    get
    {
        return this.gridCollection;
    }
    set
    {
        this.gridCollection = value;
        base.RaisedPropertyChanged("GridCollection");
    }
}

public List<string> ComboBoxList
{
    get
    {
        return this.comboBoxList;
    }
    set
    {
        this.comboBoxList = value;
        base.RaisedPropertyChanged("GridList");
    }
}

public MultiValueViewModel(string data)
{
    this.GridCollection = new ObservableCollection<ItemModel>(); 
    this.GridCollection.Add(new ItemModel("ABC", 0));
    this.ComboBoxList = new List<string>();
    //Add items to list
}

模型(模型包含一个具有 2 个属性的类):

Model (The model contains one class with 2 properties) :

public class ItemModel
{
    public ItemModel(string col1, double col2)
    {
        this.Col1 = col1;
        this.Col2 = col2;
    }

    public string Col1 { get; set; }

    public double Col2 { get; set; }
}

我尝试过 Path=ComboBoxList 和 DataContext.ComboBoxList - 两者都不起作用.

I've tried with Path=ComboBoxList and DataContext.ComboBoxList - bit both don't work.

推荐答案

试试这个:

<ComboBox ItemsSource="{Binding Path=DataContext.ComboBoxList, RelativeSource={RelativeSource AncestorType=DataGrid}}" BorderThickness="0" BorderBrush="Transparent" SelectedValue="{Binding Col1, Mode=TwoWay}"/>

ComboBoxDataContext 默认是 ItemModel,所以你应该绑定到 DataContext 的一个属性> 父 DataGrid.您可以按照上述使用 {RelativeSource} 执行此操作.

The DataContext of the ComboBox is the ItemModel by default so you should bind to a property of the DataContext of the parent DataGrid. You could do this using a {RelativeSource} as per above.

这篇关于DataGrid 内 ComboBox 上的 ItemSource 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆