WPF-DataGridComboboxColumn中的绑定错误 [英] WPF - Binding error in DataGridComboboxColumn

查看:103
本文介绍了WPF-DataGridComboboxColumn中的绑定错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的dataGrid中使用 DataGridComboboxColumn
但是我要绑定到datagrid的可观察集合不是静态的吗?

I am in a need of using DataGridComboboxColumn in my dataGrid. But the observable collection I am going to bind to my datagrid is not static ?

由于数据网格包含该Observale集合是否必须是静态的? DataGridComboboxColumn

is it necessary for that Observale collection to be static since the data grid contains a DataGridComboboxColumn.

我要绑定到 DataGridCombobox 列是该类中包含的另一个集合,我上面提到的可观察集合中包含对象。这是我第一次使用DataGridCombobox列。

What I am going to bind to the DataGridCombobox column is another collection contained in the class of which objects are there in the observable collection I mentioned above. This is the first time I am using DataGridCombobox column. Any explanation will be much appreciated.

<DataGrid ItemSource="{Binding SomeData}">
    <DataGrid.Columns>
        <DataGridTemplateColumn DisplayMemberPath="FirstName" />
        <DataGridTemplateColumn DisplayMemberPath="LastName"  />
        <DataGridComboboxColumn ItemSource={Binding SomeOtherListContainedInSomeDataAbove}" />
    </DataGrid.Columns>
<DataGrid>

但这会产生绑定错误,并说找不到 SomeOtherListContainedInSomeDataAbove

But this generates binding errors and says that SomeOtherListContainedInSomeDataAbove cannot be found.

请帮忙。

推荐答案

DataGridComboboxColumn 不参与逻辑树,因此很难直接为其 ItemsSource 绑定,但是 DataGridCell 是逻辑树的一部分。您可以创建一个元素设置样式并尝试绑定您的集合,在下面的示例中,我给出了两种使用 DataGridComboBoxColumn DatagridTemplateColumn 的方法。假设您正在使用MVVM。

DataGridComboboxColumn does not Participate in Logical Tree and hence it hard to do bind for its ItemsSource Directly. But DataGridCell is part of the Logical Tree. You can Create an Element Style and try to bind your collection. In the below sample I have given two approaches using DataGridComboBoxColumn and DatagridTemplateColumn. I am assuming you are using MVVM.

<DataGrid ItemsSource="{Binding DataGrdList}" AutoGenerateColumns="False">            
        <DataGrid.Columns>
            <DataGridComboBoxColumn SelectedItemBinding="{Binding DataTypeName}"  Header="Combo">
                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.DataTypeList}"/>
                        <Setter Property="IsReadOnly" Value="True"/>
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.DataTypeList}"/>
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>
            <DataGridTemplateColumn Header="Template">
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox SelectedValue="{Binding Path=DataTypeName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                  ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},Path=DataContext.DataTypeList}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=DataTypeName, UpdateSourceTrigger=PropertyChanged}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>

    </DataGrid>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MainViewModel();
    }
}

public class MainViewModel
{
    private ObservableCollection<string> myVar = new ObservableCollection<string>();

    public ObservableCollection<string> DataTypeList
    {
        get { return myVar; }
        set { myVar = value; }
    }

    private ObservableCollection<DataType1> myVar1 = new ObservableCollection<DataType1>();

    public ObservableCollection<DataType1> DataGrdList
    {
        get { return myVar1; }
        set { myVar1 = value; }
    }

    public MainViewModel()
    {
        myVar.Add("Int");
        myVar.Add("Bool");
        myVar.Add("String");  
    }   
}

public class DataType1
{
    private string name;

    public string DataTypeName
    {
        get { return name; }
        set { name = value; }
    }  
}

这篇关于WPF-DataGridComboboxColumn中的绑定错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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