Isaiah Nelson
Isaiah Nelson

Reputation: 2490

Conditionally set a GridViewColumn Cell Template

I have a view DisplayTestsView.cs and in that view I have

<ListView ItemsSource="{Binding SelectedTechnician.Tests}"
SelectedItem="{Binding SelectedTest}" x:Name="AvailableTestsListView" Height="140">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Id" Width="auto" DisplayMemberBinding="{Binding Id}"/>
            <GridViewColumn Header="Test" Width="auto" DisplayMemberBinding="{Binding TestTypeName}"/>
            <GridViewColumn Header="Status" Width="auto" DisplayMemberBinding="{Binding StutusTypeName}"/>
        </GridView>
    </ListView.View>
</ListView>

I want the TextColor of the StatusTypeName Cell text to be a certain color based on a condition. For example, if the StatusTypeName equals "Incomplete" then I would like the text in the cell turn red.

The list of Tests (entities) as one can see from the view is the SelectedTechnician.Tests and SelectedTechnician is a property on my ViewModel (DisplayTestsViewModel.

I imagine one has to work with the CellTemplate property on GridViewColumn, but having little insight in how to conditionally manipulate Templates (in general), I am not sure where to start.

Even if the CellTemplate has nothing to do with it how can I coniditionally set the text of a GridViewColumn's cell to change color based on a condition as similarly described above?

Upvotes: 3

Views: 1914

Answers (1)

devuxer
devuxer

Reputation: 42374

You are correct that the CellTemplate is the right place for implementing a color change.

You could do something like this (warning - untested):

<ListView ItemsSource="{Binding SelectedTechnician.Tests}"
SelectedItem="{Binding SelectedTest}" x:Name="AvailableTestsListView" Height="140">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Id" Width="auto" DisplayMemberBinding="{Binding Id}"/>
            <GridViewColumn Header="Test" Width="auto" DisplayMemberBinding="{Binding TestTypeName}"/>
            <GridViewColumn Header="Status" Width="auto">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock
                            Text="{Binding StatusTypeName}"
                            Foreground="{Binding StatusTypeName, Converter={StaticResource StatusTypeNameToBrushConverter}}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
        </GridView>
    </ListView.View>
</ListView>

StatusTypeNameToBrushConverter would be something like this:

public class StatusTypeNameToBrushConverter : IValueConverter
{
    public object Convert(
        object value, Type targetType, object parameter, CultureInfo culture)
    {
        var statusTypeName = (string)value;
        switch (statusTypeName)
        {
            case "Incomplete":
                return Brushes.Red;
            default:
                return Brushes.Black;
        }
    }

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

Upvotes: 5

Related Questions