0

I'm trying to create a grid view with 3 columns, these are taken from a struct:

    public struct ColorAttribute
    {
        public Color Color { get; set; }

        public string TextValue { get; set; }

        public int Count { get; set; }
    }

And then I want to build a the table like so:

    <ListView ItemsSource="{Binding ColorAttributes}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Color" Width="120" DisplayMemberBinding="{Binding Color}"/>
                <GridViewColumn Header="Color Name" Width="120" DisplayMemberBinding="{Binding TextValue}" />
                <GridViewColumn Header="Count" Width="50" DisplayMemberBinding="{Binding Count}" />
            </GridView>
        </ListView.View>
    </ListView>

The part I'm not sure about though, is that for the first column, I want to just display a little box of the color, but I'm not sure how to bind the DisplayMemberBinding to something else like the foreground. Currently it just shows as the RGB value again.

djcmm476
  • 1,723
  • 6
  • 24
  • 46
  • Simply customize cell template of corresponding column and [bind color](https://stackoverflow.com/q/3309709/1997232) to background. – Sinatr Feb 03 '21 at 16:10
  • If I'm reading it correctly that issue is talking about setting a column to a preset colour. I'm asking how to set the colour per cell based on the value inside the struct. – djcmm476 Feb 03 '21 at 16:11
  • Converting it a brush isn't the issue, it's how to get the display member binding and use that as a color input for the foreground. – djcmm476 Feb 03 '21 at 16:12
  • Just bind to `Color`. You can remove `DisplayMemberBinding` from that column definition. – Sinatr Feb 03 '21 at 16:16

1 Answers1

2

You can define template for the cell and inside the DataTemplate bind Background or Foreground to Color in your view model. You have to create a converter to convert Color to Brush.

<Windows.Resources>
    <local:ColorToBrushConverter x:Key="ColorToBrushConverter" />
</Window.Resources>
...
<ListView ItemsSource="{Binding ColorAttributes}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Color" Width="120">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Border Margin="0,0,5,0" Width="20" 
                             Background="{Binding Color, Converter={StaticResource ColorToBrushConverter}}"/>
                            <TextBlock Text="{Binding Color}" 
                             Foreground="{Binding Color, Converter={StaticResource ColorToBrushConverter}}"/>
                        </StackPanel>                                
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Color Name" Width="120" DisplayMemberBinding="{Binding TextValue}" />
            <GridViewColumn Header="Count" Width="50" DisplayMemberBinding="{Binding Count}" />
        </GridView>
    </ListView.View>
</ListView>

Color to brush converter

enter image description here

user2250152
  • 14,658
  • 4
  • 33
  • 57