如何将单元格值绑定到对象的特定属性?

发布于 2024-10-22 19:48:49 字数 413 浏览 1 评论 0原文

我有一个在单元格级别填充以下类型的 DataTable

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

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

绑定片段:

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

I have a DataTable that is populated at the cell level with the following type:

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

I've bound the DataTable to a WPF Toolkit DataGrid, but the cells are coming up empty. How do I tell the DataGrid to look in the Value property of my GridCell type for the cell contents?

Binding snippet:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪痕残 2024-10-29 19:48:50

根据您问题中的有限信息,我猜测您的数据表包含我不推荐的自定义类型列,因为它会使您的代码变得更加复杂。但如果您决定这样做,您可以实现自定义列类型来处理该问题。我还没有尝试过,但这个链接看起来很有希望: 以编程方式使用 DataGridTemplateColumn 将 DataTable 绑定到 WPF DataGrid

您还可以使用值转换器来实现相同的目的。下面的示例将列索引作为参数传递,但您也可以使用某种格式传递您感兴趣的属性(例如 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();
        }
    }
}

From the limited info in your question I am guessing that your data table contains columns of custom types which I would not recommend since it will make your code so much more complicated. But if you decide to go that way you can implement your custom column type to handle that. I haven't tried that, but this link looks promising: Bind DataTable to WPF DataGrid using DataGridTemplateColumn Programatically

You can also use value converters to achieve the same thing. Below sample pass the column index as parameter but you could also pass which property you are interested in as well using some format (0-Value for example, meaning column 0, property Value).

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>

Code behind:

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();
        }
    }
}
固执像三岁 2024-10-29 19:48:50

尝试 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>

Try 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文