【问题标题】:How to bind WPF DataGrid to ObservableCollection如何将 WPF DataGrid 绑定到 ObservableCollection
【发布时间】:2014-08-23 17:03:40
【问题描述】:

你能告诉我如何将 WPF DataGrid 绑定到 ObservableCollection。我看过一些帖子,但没有找到直接的答案。到处都描述了复杂的问题,但我的问题并不复杂。我有一个可观察的集合和 WPF DataGrid。它们都在 WPF 应用程序中,它是双工合同 WCF 服务的客户端。 这是一个 ObservableCollection:

private ObservableCollection<MyClass> _myCollection = new ObservableCollection<MyClass>();
public ObservableCollection<MyClass> DownloadsCollection
{
    get { return this._downloadsCollection; }
}

这是一个带有 DataGrid 的 XAML 标记:

<Window x:Class="DownloadManager_Client.MainWindow"
. . . . . . . .>

    <DataGrid Name="dgDownloadsInfo" Grid.Row="2" Grid.Column="0" AutoGenerateColumns="False" CanUserAddRows="False"
              CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserResizeColumns="False"
              CanUserResizeRows="False" CanUserSortColumns="False" SelectionMode="Single" SelectionChanged="dgDownloadsInfo_SelectionChanged">
          <DataGrid.Columns>
                <DataGridTextColumn Header="DownloadId" Visibility="Hidden"/>
                <DataGridTextColumn Header="Target URL" FontFamily="Arial" />
                <DataGridTextColumn Header="Content Size" FontFamily="Arial"/>
                <DataGridTextColumn Header="Path to Save" FontFamily="Arial"/>
                <DataGridTextColumn Header="Bytes Downloaded" FontFamily="Arial"/>
                <DataGridTextColumn Header="Percent (%)" FontFamily="Arial"/>
                <DataGridTextColumn Header="Status" FontFamily="Arial"/>
          </DataGrid.Columns>
    </DataGrid>
. . . . . . . .
</Window>

这里是 myClass 类。它在 WCF 服务中实现。客户端从具有双工合同的 WCF 服务的回调中接收 MyClass 的实例。在接收到 MyClass 的每个实例后,将其放入 ObservableCollection 以用相同的唯一标识符替换前一个。

[DataContract]
public class MyClass
{
    #region Properties

    /// <summary>
    /// Downloading unique ID.
    /// </summary>
    [DataMember]
    public Guid UniqueId { get; set; }
    /// <summary>
    /// Target URL.
    /// </summary>
    [DataMember]
    public String TargetUrl { get; set; }
    /// <summary>
    /// Path to Save.
    /// </summary>
    [DataMember]
    public String PathToSave { get; set; }
    /// <summary>
    /// Percentage.
    /// </summary>
    [DataMember]
    public Int32 Percentage { get; set; }
    /// <summary>
    /// Downloaded bytes number.
    /// </summary>
    [DataMember]
    public Int64 DownloadedBytesQuantity { get; set; }
    /// <summary>
    /// Content size.
    /// </summary>
    [DataMember]
    public Int64 RealContentLength { get; set; }
    /// <summary>
    /// Downloading status.
    /// </summary>
    [DataMember]
    public String Status { get; set; }

    #endregion
}

在我的示例中,如何将 DataGrid 绑定到 ObservableCollection?给这个话题一个提示。请原谅我的英语不好。

【问题讨论】:

  • 有什么问题?你读过stackoverflow.com/questions/15087131/…吗?您的 ObservableCollection 属性位于哪个类中?
  • ObservableCollection 位于 WPF 应用程序的主窗口类中,它是 WCF 服务的客户端,DataGrid 也是如此。
  • 我也有同样的问题。由于某种原因,数据协定类不能与 observablecollection 一起使用。我改成简单的类,它起作用了……@user3769902

标签: c# wpf wcf


【解决方案1】:

您应该可以通过使用网格的ItemsSource 属性并引用您的集合(可能位于您的视图模型中)来做到这一点,如下所示:

ItemsSource="{Binding Path=DownloadsCollection}" 

然后在列上添加绑定以显示集合中 MyClass 对象的信息(属性)。

有关如何操作的更详细教程,请查看this 链接。

编辑:

您可以简单地尝试这样的操作,看看是否一切正常,然后转到自定义列:

<DataGrid ItemsSource="{Binding DownloadsCollection}" />

【讨论】:

  • 这样吗?
  • 是的,但不要忘记绑定列上的值。要么让网格自动生成列(它的另一个属性),然后删除你自己的声明。
  • 请写出如何绑定列上的值。不,不,我很清楚。谢谢。
  • 它应该是这样的: 检查我在帖子上的链接,它解释了你需要的一切知道。
【解决方案2】:
<DataGrid x:Name="employeeGrid" HorizontalAlignment="Center" VerticalAlignment="Center" AlternatingRowBackground="LightBlue" AlternationCount="2" AutoGenerateColumns="False" ItemsSource="{Binding}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Emp #" Binding="{Binding EmpId}"/>
        <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
        <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>
    </DataGrid.Columns>
</DataGrid>

以下内容进入您相应的 .cs 文件:

employeeGrid.ItemsSource = employeeDetails;

【讨论】:

    【解决方案3】:

    Datagrid binding in WPF 检查这个答案。 基本上您需要添加ItemSource 绑定,以便您的网格知道数据上下文。

    你需要为datagrid columns 添加绑定,所以它知道要显示什么。 希望这有帮助。

    此外,如有必要,您可能希望为您的DownloadsCollectionbinding mode 添加setter。如果您需要一些更新,这将很有帮助。

    【讨论】:

    • 我不需要自己设置 ObservableCollection。我只需要设置它的项目。谢谢。
    • 对不起,我不明白)您需要将DownloadsCollection 设置为数据上下文。 DataGrid 本身足够聪明,可以遍历 datacontext 并使用绑定来显示items。是你需要的吗?
    【解决方案4】:

    您可以像这样填充动态数据网格:

    ObservableCollection<CaseItem> data = new ObservableCollection<CaseItem>();
    this.CasesDataGrid.ItemsSource = data;
    

    但不要忘记将列与班级的每个项目绑定。

    XAML 代码应该是这样的:

    <DataGrid x:Name="CasesDataGrid" Grid.Row="1" Grid.Column="0" AutoGenerateColumns="False" SelectionUnit="FullRow" SelectionMode="Extended" CanUserAddRows="False" GridLinesVisibility="Horizontal">
    <DataGrid.Columns>
    <DataGridTextColumn Width="*" Header="ID" Binding="{Binding CaseID}"/>
    <DataGridTextColumn Width="*" Header="Date" Binding="{Binding CaseDate}"/>
    <DataGridTextColumn Width="*" Header="Plate" Binding="{Binding CasePlate}"/>
    <DataGridTextColumn Width="*" Header="Candidate" Binding="{Binding CaseCandidate}"/>
    <DataGridTextColumn Width="*" Header="Base" Binding="{Binding CaseBase}"/>
    <DataGridTextColumn Width="*" Header="Speed" Binding="{Binding CaseSpeed}"/>
    <DataGridTextColumn Width="*" Header="Photo" Binding="{Binding CasePhoto}"/>
    </DataGrid.Columns>
    </DataGrid>
    

    希望对你有用。

    【讨论】:

      猜你喜欢
      • 2013-10-14
      • 2016-09-01
      • 2017-02-08
      • 2015-01-26
      • 2015-04-02
      • 2016-08-28
      • 2013-09-07
      • 2017-04-16
      相关资源
      最近更新 更多