内容不会出现在datagrid中

时间:2017-01-09 02:13:23

标签: c# wpf data-binding wpfdatagrid

好吧,我有一个包含这样的数据对象的数据类:

    private ObservableCollection<bool> check = new ObservableCollection<bool>();
    public ObservableCollection<bool> Check
    {
        get { return check; }
        set
        {
            check = value;
            Notify("check");
        }
    }

    private ObservableCollection<string> user = new ObservableCollection<string>();
    public ObservableCollection<string> User
    {
        get { return user; }
        set
        {
            user = value;
            Notify("user");
        }
    }

在MainWindow中,我添加了一个像这样的DataGrid:

    <DataGrid AutoGenerateColumns="False" 
      Name="dataGrid1" 
      CanUserAddRows="False" CanUserSortColumns="False" CanUserResizeColumns="True" CanUserReorderColumns="False" 
      ItemsSource="{Binding}">
        <DataGrid.Columns >
            <DataGridCheckBoxColumn Header = "" Binding="{Binding Check, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" MinWidth="50" />
            <DataGridTextColumn Header = "User" Binding="{Binding User, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" MinWidth="50" />
        </DataGrid.Columns>
    </DataGrid>

对于整个Window,datakontext设置为数据类。在构造函数中,我调用了#34; DataContext = theData&#34 ;;我在数据类的构造函数中添加了一些值,并通过运行该类的实例来验证。这些值正确添加到ObservableCollection。

但是数据网格中没有显示这些值。为什么呢?

3 个答案:

答案 0 :(得分:1)

DataGrid的ItemsSource属性应设置或绑定到IEnumerable<T>。并且DataGrid中的单个列应绑定到类型T的属性。您正在尝试将DataGridTextColumn绑定到ObservableCollection<string>,将DataGridCheckBoxColumn绑定到ObservableCollection<bool>,这没有任何意义。它们应分别绑定到stringbool属性。请参阅以下示例代码。

<强>型号:

public class YourDataObject : INotifyPropertyChanged
{
    private bool _check;
    public bool Check
    {
        get { return _check; }
        set { _check = value; NotifyPropertyChanged(); }
    }

    private string _user;
    public string User
    {
        get { return _user; }
        set { _user = value; NotifyPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

查看型号:

public class ViewModel
{
    public ViewModel()
    {
        TheDataObjects = new ObservableCollection<YourDataObject>();
        TheDataObjects.Add(new YourDataObject());
        TheDataObjects.Add(new YourDataObject());
        TheDataObjects.Add(new YourDataObject());
    }
    public ObservableCollection<YourDataObject> TheDataObjects { get; private set; }
}

查看:

public MainWindow()
{
    InitializeComponent();
    DataContext = new ViewModel();
}
<DataGrid AutoGenerateColumns="False" 
  Name="dataGrid1" 
  CanUserAddRows="False" CanUserSortColumns="False" CanUserResizeColumns="True" CanUserReorderColumns="False" 
  ItemsSource="{Binding TheDataObjects}">
    <DataGrid.Columns >
        <DataGridCheckBoxColumn Header = "" Binding="{Binding Check, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" MinWidth="50" />
        <DataGridTextColumn Header = "User" Binding="{Binding User, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" MinWidth="50" />
    </DataGrid.Columns>
</DataGrid>

答案 1 :(得分:0)

尝试设置,

this.DataContext = theData;

答案 2 :(得分:0)

您需要为ItemsSource设置适当的属性。

 ItemsSource="{Binding User}"

以上行将清除此问题。 此外,您应该在Setter中通知公共属性。

Notify("Check");
Notify("User");