【问题标题】:How to properly bind a list to a WPF DataGrid如何正确地将列表绑定到 WPF DataGrid
【发布时间】:2012-07-23 08:55:14
【问题描述】:

我正在尝试使用 ItemSource 将列表绑定到数据网格:

xaml.cs

<UserControl x:Class="DDCUI.CommDiagnosisWPFCtrl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Height="800" Width="300">
    <DockPanel>
        <DataGrid DockPanel.Dock="Top" Height="300" AutoGenerateColumns="True" Name="DGComm" CanUserResizeColumns="True" IsReadOnly="True" ItemsSource="{Binding Source=dataGridRows}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="No." Binding="{Binding Number}" Width="0.1*"/>
                <DataGridTextColumn Header="Time" Binding="{Binding Time}" Width="0.1*" />
                <DataGridTextColumn Header="Protocol" Binding="{Binding Protocol}" Width="0.15*" />
                <DataGridTextColumn Header="Source" Binding="{Binding Source}" Width="0.15*" />
                <DataGridTextColumn Header="Destination" Binding="{Binding Destination}" Width="0.15*" />
                <DataGridTextColumn Header="Data" Binding="{Binding Data}" Width="0.5*" />
            </DataGrid.Columns>
        </DataGrid>
        <RichTextBox DockPanel.Dock="Bottom" Height="150" Name="RtbHexCode"/>
        <TreeView DockPanel.Dock="Bottom" Height="200" Name="TreeViewDecode"/>

    </DockPanel>
</UserControl>

代码:

public class CommDGDataSource
{
    public string Number { get; set; }
    public string Time { get; set; }
    public string Protocol { get; set; }
    public string Source { get; set; }
    public string Destination { get; set; }
    public string Data { get; set; }
}

private List<object> dataGridRows = new List<object>();

private void DGAddRow(string name, FunctionType ft)
{
    CommDGDataSource ds = new CommDGDataSource();
    ds.Protocol = name;
    ds.Source = "";
    ds.Destination = "";
    ds.Number = rowCount.ToString();
    ds.Data = "";
    ds.Time = "";
    dataGridRows.Add(ds);
}

调用 DGAddRow 时,DataGrid 不会更新其值。

我在这里做错了什么?任何帮助将不胜感激。

此外,XAML 甚至在填充数据之前预先生成空行。我希望能够只显示用项目填充的行(并将新行添加到最后一个填充行的底部)。我怎样才能做到这一点?

【问题讨论】:

    标签: c# .net wpf xaml datagrid


    【解决方案1】:

    使用 ObservableCollection 代替 List 并将 dataGridRows 公开。

    【讨论】:

      【解决方案2】:

      好的,第一让我吃惊的是dataGridRows 是私有字段。如果我没记错的话,您不能绑定到私有字段,最好将其设为公共财产。(如果我错了,请纠正我)。

      第二困扰我的是一个AutoGenerateColumns="True",不确定当AutoGenerateColumnsTrue 并且明确指定列时网格如何工作。尝试设置AutoGenerateColumns="False"

      第三个:正如@Rover 已经说过的,尝试将类型datagridRows 设置为ObservableCollection&lt;CommGDDataSource&gt;

      【讨论】:

      • 如果他声明自己的列AutoGenerateColumns属性必须设置为False
      猜你喜欢
      • 2011-03-01
      • 2011-01-13
      • 1970-01-01
      • 2013-11-12
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多