How to display particular property of collection in DataGrid Cells WPF

无人久伴 提交于 2019-12-11 16:49:55

问题


I previously asked how to display data from a

IEnumerable < IEnumberable>> collection:

Changing background color of cells in DataGrid WPF

All the original code is there. I believe I have an answer for the original question but now have a question based on that.* Rather than bind to one property 'Make', I'd rather bind my cells to the containing object "Column" but still display Make as the data in the grid. This would allow me to have a more complex ValueConverter that took into account other properties to help decide the background cell color.

I've pasted by best attempt below. The commented out code shows the working code for using the property 'Make'. All works fine. The colors are there. But you'll notice I'm not getting the makes of cars (Ford, GM, etc.) but rather the name of the class "Column". I tried playing with the XAML to add things like 'PropertyPath' 'SelectedItem' to the databinding to no avail.

Any help would be appreciated.

Code

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        ViewModel vm = new ViewModel();

        List<Column> row1 = new List<Column>()
        {
            new Column(){Make = Make.Ford,OperatingStatus =  OperatingStatus.Broken},
            new Column(){Make = Make.Honda, OperatingStatus = OperatingStatus.Unknown}
        };
        List<Column> row2 = new List<Column>()
        {
            new Column() {Make = Make.GM, OperatingStatus = OperatingStatus.Working},
            new Column() {Make = Make.Toyota, OperatingStatus = OperatingStatus.Broken}
        };

        List<List<Column>> data = new List<List<Column>>();
        data.Add(row1);
        data.Add(row2);
        vm.Data = data;
        DataContext = vm;

    }
}

public enum OperatingStatus
{
    Working = 0,
    Broken = 1,
    Unknown = 2
}

public enum Make
{
    Ford,
    Honda,
    GM,
    Toyota
}

public class Column
{
    public Make Make { get; set; }
    public OperatingStatus OperatingStatus { get; set; }
}
public class ViewModel
{
    public IEnumerable<IEnumerable<Column>>  Data { get; set; }

    public DataView MyDataTable
    {
        get
        {
            var rows = Data.Count();
            var cols = Data.First().Count();
            var t = new DataTable();
            for (var c = 0; c < cols; c++)
            {
                //t.Columns.Add(new DataColumn(c.ToString()));
                t.Columns.Add(new DataColumn(c.ToString(),typeof(StackOverFlowDataGridQuestion.Column)));
            }

            foreach (var row in Data)
            {
                var newRow = t.NewRow();
                int c = 0;
                foreach (var col in row)
                {
                    newRow[c] = col; //col.Make; 
                    c++;
                }
                t.Rows.Add(newRow);
            }
            return t.DefaultView;
        }
    }
}



public class ConverterHoldoffGridColor : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values[1] is DataRow)
        {
            var cell = (DataGridCell) values[0];
            var row = (DataRow) values[1];
            var columnName = cell.Column.SortMemberPath;
            //string input = (row[columnName] as string);
            string input = (row[columnName] as StackOverFlowDataGridQuestion.Column).Make.ToString();

            switch (input)
            {
                case "Ford":
                    return Brushes.LightGreen;
                case "GM":
                    return Brushes.Red;
                case "Toyota":
                    return Brushes.Blue;
                case "Honda":
                    return Brushes.Yellow;
                default:
                    return DependencyProperty.UnsetValue;
            }
        }
        else
        {
            return SystemColors.AppWorkspaceColor;
        }

    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML

    <local:ConverterHoldoffGridColor x:Key="bgHoldoffGridColor" />
    <Style x:Key="CellHighlighterStyle">


        <Setter Property="DataGridCell.Background">
            <Setter.Value>
                <MultiBinding
                Converter="{StaticResource bgHoldoffGridColor}" >
                    <MultiBinding.Bindings>
                        <Binding RelativeSource="{RelativeSource Self}"/>
                        <Binding Path="Row" Mode="OneWay"/>
                    </MultiBinding.Bindings>
                </MultiBinding>
            </Setter.Value>
        </Setter>


    </Style>
</Window.Resources>
<Grid>
    <ScrollViewer>
        <DataGrid x:Name="myDataGrid" CellStyle="{StaticResource CellHighlighterStyle}" 
            DataContext="{Binding }"                      
                  ItemsSource="{Binding MyDataTable}">


        </DataGrid>

    </ScrollViewer>
</Grid>

And what this produces:

  • You'll notice I posted an answer to: Changing background color of cells in DataGrid WPF

but have not accepted it yet. I suspect there is a slicker way that doesn't involve using the DataView or DataGrid. Feel free to browse that question too :)

来源:https://stackoverflow.com/questions/49888969/how-to-display-particular-property-of-collection-in-datagrid-cells-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!