【问题标题】:How do I bind cell value to a specific property on an object?如何将单元格值绑定到对象上的特定属性?
【发布时间】:2011-07-15 19:54:43
【问题描述】:

我有一个DataTable,它在单元格级别使用以下类型填充:

public class GridCell
{
    public string Value { get; set}
    public Guid Id { get; set; }
}

我已将 DataTable 绑定到 WPF Toolkit DataGrid,但单元格显示为空。如何告诉DataGrid 在我的GridCell 类型的Value 属性中查找单元格内容?

绑定sn-p:

<DataGrid ItemsSource="{Binding SelectedItem.Table}" />

【问题讨论】:

  • 您是否将网格的 itemsource 属性分配给 GridCell 集合。您能否发布您的绑定代码。

标签: wpf xaml datagrid binding wpftoolkit


【解决方案1】:

根据您问题中的有限信息,我猜您的数据表包含自定义类型的列,我不建议这样做,因为它会使您的代码更加复杂。但是,如果您决定采用这种方式,则可以实现自定义列类型来处理它。我没有尝试过,但这个链接看起来很有希望:Bind DataTable to WPF DataGrid using DataGridTemplateColumn Programatically

您也可以使用值转换器来实现相同的目标。下面的示例将列索引作为参数传递,但您也可以使用某种格式(例如,0-Value,表示第 0 列,属性值)传递您感兴趣的属性。

XAML:

<Window x:Class="GridCellDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    xmlns:GridCellDemo="clr-namespace:GridCellDemo"
    Title="Window1" Height="300" Width="300">

    <Window.Resources>
        <GridCellDemo:CellConverter x:Key="CellConverter" />
    </Window.Resources>

    <Grid>
        <Controls:DataGrid ItemsSource="{Binding Data}" AutoGenerateColumns="False">
            <Controls:DataGrid.Columns>
                <Controls:DataGridTextColumn Header="Col 0" Binding="{Binding ., Converter={StaticResource CellConverter}, ConverterParameter=0}" />
                <Controls:DataGridTextColumn Header="Col 1" Binding="{Binding ., Converter={StaticResource CellConverter}, ConverterParameter=1}" />
            </Controls:DataGrid.Columns>
        </Controls:DataGrid>

    </Grid>
</Window>

后面的代码:

using System;
using System.Data;
using System.Windows;
using System.Windows.Data;

namespace GridCellDemo
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            DataContext = this;
        }

        public DataTable Data
        {
            get
            {
                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("Col0", typeof(GridCell)));
                dt.Columns.Add(new DataColumn("Col1", typeof(GridCell)));

                DataRow row0 = dt.NewRow();
                dt.Rows.Add(row0);
                row0["Col0"] = new GridCell() { Value = "R0C0" };
                row0["Col1"] = new GridCell() { Value = "R0C1" };

                DataRow row1 = dt.NewRow();
                dt.Rows.Add(row1);
                row1["Col0"] = new GridCell() { Value = "R1C0" };
                row1["Col1"] = new GridCell() { Value = "R1C1" };

                return dt;
            }
        }
    }

    public class GridCell
    {
        public string Value { get; set;  }
        public Guid Id { get; set; }
    }

    public class CellConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            DataRowView drv = value as DataRowView;
            if (drv == null)
            {
                return string.Empty;
            }
            int columnIndex = int.Parse(parameter.ToString());
            GridCell gridCell = drv[columnIndex] as GridCell;
            return gridCell.Value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

【讨论】:

  • 转换器听起来可行。谢谢!
【解决方案2】:

试试ItemsSource="{Binding Path=GridCellList}"

<DataGrid ItemsSource="{Binding GridCellList}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Value" Binding="{Binding Path=Value}" />
         <DataGridTextColumn Header="Guid" Binding="{Binding Path=Guid}" />
     </DataGrid.Columns>
 </DataGrid>

【讨论】:

  • 行和列与当前绑定一起出现。是空的单元格。
  • 您使用 DataTable 是否有任何具体原因。你不能直接绑定GridCell的集合吗?我还没有尝试将 dataTable 绑定到 dataGrid。
  • 收藏不是问题。我需要绑定到集合中项目的特定属性。
  • 然后你可以手动定义网格的列。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-28
  • 1970-01-01
  • 2016-02-28
  • 2013-04-21
  • 2021-10-23
相关资源
最近更新 更多