双向绑定数据网格WPF XAML [英] Twoway binding datagrid wpf xaml

查看:57
本文介绍了双向绑定数据网格WPF XAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将CLR属性绑定到datagrid行中如何在datagrid中绑定行绑定(双向模式)CodeBehind:

I want to bind CLR property into datagrid Row How to Bind row binding in datagrid(Twoway Mode) CodeBehind:

public partial class TechnicalPropertiesU_Var : Window
    {
        public TechnicalPropertiesU_Var()
        {
            InitializeComponent();
            List<Myclass> myclassList = new List<Myclass>();
            myclassList.Add(new Myclass() { IA = 0, IB = 0, IC = 0, ID = 0, IE = 0, IF = 0, IF = 0 });
            MyGrid.ItemsSource = myclassList;
        }
    }

MyWindow Xaml:

MyWindow Xaml:

<Grid>
        <DataGrid x:Name="MyGrid" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Argument Name"></DataGridTextColumn>
                <DataGridTextColumn Header="Argument Value"></DataGridTextColumn>
            </DataGrid.Columns>
            
        </DataGrid>
    </Grid>

模型类

public class Myclass
{
private int iA;
private int iB;
private int iC;
private int iD;
private int iE;
private int iF;
private int iG;

public int IA{get=>iA; set=>iA =value;}
public int IB{get=>iB; set=> iB =value;}
public int IC{get=>iC; set=> iC =value;}
public int ID{get=>iD; set=> iD =value;}
public int IE{get=>iE; set=> iE =value;}
public int IF{get=>iF; set=> iF =value;}
public int IG{get=>iG; set=> iG =value;}
}

如何获取datagrid字段中的特定列.请给任何样品

How to get particular column in datagrid field. please give any samples

推荐答案

如果您尝试获得的结果是一个包含两列的表(请参见您的图像),那么您显然很容易混淆行和列.您的数据类必须具有两个属性: ArgumentName ArgumentValue .您必须知道每个项目(或 MyClass 的每个实例)将显示为一行.现在,为每一行创建一个 MyClass 的实例,并将其添加到源集合中.将 DataGridTextColumn.Binding 属性绑定到 MyClass 模型的列的相关属性.

If the result you are trying to get is a table with two columns (see your image) then you obviously mixed up rows and columns I guess. You data class must have two properties: ArgumentName and ArgumentValue. You must know that each item (or each instance of MyClass) will be displayed as a row. Now, for each row create an instance of of MyClass and add it to the source collection. The bind the DataGridTextColumn.Binding property to the column's related property of the MyClass model.

以下示例将显示图片中的表格:

The following example will show the table from your image:

MyTableClass.cs

public class MyTableClass : INotifyPropertyChanged
{
    public MyTableClass(int argumentName, int argumentValue)
    {
        this.ArgumentName = argumentName;
        this.ArgumentValue = argumentValue;
    }

    private int argumentName;   
    private int argumentValue;

    public int ArgumentName{get=>argumentName; set{argumentName=value; NotifyPropertyChanged();}}
    public int ArgumentValue{get=>argumentValue; set{argumentValue=value; NotifyPropertyChanged();}}

    public event PropertyChangedEventHandler PropertyChanged;

    // This method is called by the Set accessor of each property.
    // The CallerMemberName attribute that is applied to the optional propertyName
    // parameter causes the property name of the caller to be substituted as an argument.
    private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

技术属性U_Var.xaml.cs

public partial class TechnicalPropertiesU_Var : Window
{
    public TechnicalPropertiesU_Var()
    {
        InitializeComponent();
        List<MyTableClass> myTableClasses = new List<MyTableClass>();

        myTableClasses.Add(new MyTableClass() { ArgumentName = "IA", ArgumentValue = 5 });
        myTableClasses.Add(new MyTableClass() { ArgumentName = "IB", ArgumentValue = 3 });
        myTableClasses.Add(new MyTableClass() { ArgumentName = "IC", ArgumentValue = 2 });
        myTableClasses.Add(new MyTableClass() { ArgumentName = "ID", ArgumentValue = 0 });
          
        MyGrid.ItemsSource = myTableClasses;
    }
}

MyWindow.xaml

<Grid>
    <DataGrid x:Name="MyGrid" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Argument Name" Binding="{Binding ArgumentName}"></DataGridTextColumn>
            <DataGridTextColumn Header="Argument Value" Binding="{Binding ArgumentValue}"></DataGridTextColumn>
        </DataGrid.Columns>
        
    </DataGrid>
</Grid>


要发表您的评论:我不想绑定参数名和参数值属性.我想绑定我自己的CLR属性.因为我有250个变量."


To address your comment: "i dont want to bind argumentname and argument value property. i want to bind my own CLR property. because i have 250 variables."

我不必了解您的意图就可以告诉您这是一个坏主意.一个类不应具有250个属性.您的图片清楚地表明,并非所有250个属性都具有关联,但是只有两个属性具有关联.

I don't have to understand your intentions to tell you that this is a bad idea. A class shouldn't have 250 properties. Your image shows clearly that not all 250 properties have a relation, but only two have.

在您的情况下,数据由字符串值(例如"IA" )和数字值(例如)组成. 5 .您应该更改班级以反映这一点.
您应该知道,在常见的关系数据库设计中,数据记录由一行表示.每行是一个对象,此行的每一列都是该对象的属性. DataGrid´的设计意图相同:每一行都是 DataGrid.ItemsSource`的一项.每个项目都是一个单独的实例.通常从左到右读取任何表格,例如产品价格表,其中一行的每一列都期望与单个数据项相关,例如产品价格表.产品.

In your case a datum consists of a string value e.g., "IA" and a numeric value e.g. 5. You should change your class to reflect this.
You should know that in common relational database design a data record is represented by a row. Each row is an object and each column of this row an attribute of this object. The DataGrid´ is designed with the same intention: each row is an item of the DataGrid.ItemsSource`. Each item is a separate instance. It is common to read any table e.g., a price table of products, from left to right, where each column of a row is expected to relate to a single data item e.g. product.

我强烈建议放弃您的初衷,并更改数据类以反映数据的关系,例如示例的 MyTableClass 反映具有两个属性(列)的数据对象(行)的关系

I highly recommend to drop your initial intention and change your data classes to reflect the relationship of your data like the example's MyTableClass reflects the relation of a data object (row) with two attributes (columns).

这篇关于双向绑定数据网格WPF XAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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