开发者

How do I bind cell value to a specific property on an object?

开发者 https://www.devze.com 2023-02-17 11:11 出处:网络
I have a DataTable that is populated at the cell level with the following type: public class GridC开发者_如何学Goell

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

public class GridC开发者_如何学Goell
{
    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}" />


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();
        }
    }
}


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号