简体   繁体   English

C#列表绑定到WPF DataGrid

[英]c# list binding to wpf datagrid

I started to work with WPF's datagrid and faced with the problem. 我开始使用WPF的datagrid并遇到了问题。 I have such list 我有这样的清单

private List<ChainCode> _chainCode = new List<ChainCode>();

where ChainCode is: 其中ChainCode是:

public class ChainCode
{
    private uint _number;
    private byte _code;

    public uint Number { get { return _number; } set { _number = value; } }
    public byte Code { get { return _code; } set { _number = value; } }
}

So, I want to bind id to such DataGrid: 因此,我想将ID绑定到此类DataGrid:

<DataGrid x:Name="dataGridChainCode" ItemsSource="{Binding _chainCode}" CanUserAddRows="True" IsEnabled="False" AutoGenerateColumns="False" Margin="10,35,18,0" VerticalAlignment="Top">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Number}" IsReadOnly="True" Header="#" Width="60"/>
        <DataGridTextColumn Binding="{Binding Code}" Header="code" Width="60"/>
    </DataGrid.Columns>
</DataGrid>

but when I launch my program, I can't add any row to datagrid, cuz there is no row, only headers. 但是,当我启动程序时,我无法向datagrid添加任何行,因为没有行,只有标题。 And I just have no idea how to fix it. 而且我不知道如何解决它。


Update: I made such changes: 更新:我进行了这样的更改:

private ObservableCollection<ChainCode> _chainCode = new ObservableCollection<ChainCode>();
public ObservableCollection<ChainCode> OCChainCode { get { return _chainCode; } set { _chainCode = value; } }

and next in xaml: 然后在xaml中:

ItemsSource="{Binding OCChainCode}"

but there is no response. 但没有回应。 What I doing wrong? 我做错了什么?


The one thing i understood is than it's too early for me to use WPF. 我了解的一件事是,现在使用WPF还为时过早。 So, I'll try to fix it later 所以,我稍后会尝试修复

You can't bind to non-public member. 您无法绑定到非公开成员。 Create property 建立属性

public List<ChainCode> ChainCodeList
{
    get { return _chainCode;}
    set { _chainCode = value;}
}

and bind to it. 并绑定到它。

If your list will change at app lifetime you can use ObservableCollection<T> instead. 如果您的列表在应用程序生命周期内发生变化,则可以改用ObservableCollection <T>

Use ObservableCollection instead of List and implement INotifyPropertyChanged , if you want the list to update when you change values of Number or Code. 如果要在更改Number或Code的值时更新列表,请使用ObservableCollection而不是List并实现InotifyPropertyChanged

And make your list property public or protected. 并将您的列表属性设为公开或受保护。

In order to use data binding, you need to set the DataContext of the part where you want to use the binding. 为了使用数据绑定,您需要在要使用绑定的位置设置零件的DataContext。 There are several different ways to set the DataContext, a simple way to do it is in the constructor of the UserControl-class. 设置DataContext有几种不同的方法,一种简单的方法是在UserControl类的构造函数中。 Usually the DataContext is set to a ViewModel (Using the Model-View-ViewModel-pattern), but it can be any class. 通常将DataContext设置为ViewModel(使用Model-View-ViewModel-pattern),但是它可以是任何类。

public class ViewModel {
   private ObservableCollection<ChainCode> _chainCode = new ObservableCollection<ChainCode>();

   public ObservableCollection<ChainCode> OCChainCode 
   { 
       // No need for a public setter
       get { return _chainCode; }        
   }
}

UserControl-class (the class in [name].xaml.cs file): UserControl-class([name] .xaml.cs文件中的类):

public class MyUserControl : UserControl
{
   public MyUserControl()
   {
      DataContext = new ViewModel();
   }
}

You can now use the xaml in your question. 现在,您可以在问题中使用xaml。

<DataGrid x:Name="dataGridChainCode" ItemsSource="{Binding _chainCode}" CanUserAddRows="True" IsEnabled="False" AutoGenerateColumns="False" Margin="10,35,18,0" VerticalAlignment="Top">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Number}" IsReadOnly="True" Header="#" Width="60"/>
                <DataGridTextColumn Binding="{Binding Code}" Header="code" Width="60"/>
            </DataGrid.Columns>
        </DataGrid>

A thing to note, there are no automatic functionality to add new rows in a DataGrid, you have to write this code yourself. 需要注意的是,没有自动功能可以在DataGrid中添加新行,您必须自己编写此代码。 I would suggest a button that creates a new ChainCode-object and adds it to the Observable collection, then it will show up in the UI. 我建议创建一个新的ChainCode对象并将其添加到Observable集合中的按钮,然后将其显示在UI中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM