Search code examples
c#wpflistviewtextcolor

I want to change the color in a WPF ListView when column contains certain words


I know how to change colors if certain values match, as shown below.

How can I do for change foreground color not only same, but also values are included.

(Like String.Contain(Value))

<GridViewColumn Header="Permission" Width="170" >
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding permission}">
                <TextBlock.Style>
                    <Style TargetType="{x:Type TextBlock}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding permission}" Value="Pass">
                                <Setter Property="Foreground" Value="#4c72cc"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding permission}" Value="Fail">
                                <Setter Property="Foreground" Value="#ef6eab"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

Solution

  • To achieve that there are two ways:

    Method 1

    Either define two converters that check whether your string contains the "Pass"/"Fail" values:

    public class PassConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (value as string)?.Contains("Pass");            
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class FailConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (value as string)?.Contains("Fail");
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    Add the converters to your static resources:

    <Window.Resources>
        <YourNameSpace:PassConverter x:Key="PassConverter"></local:PassConverter>
        <YourNameSpace:FailConverter x:Key="FailConverter"></local:FailConverter>
    </Window.Resources>
    

    And update your Triggers to use them:

    <GridViewColumn Header="Permission" Width="170" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Permission}">
                                    <TextBlock.Style>
                                        <Style TargetType="{x:Type TextBlock}">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding Path=permission, Converter={StaticResource PassConverter}}">
                                                    <DataTrigger.Value>True</DataTrigger.Value>
                                                    <Setter Property="Foreground" Value="#4c72cc"/>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding permission,Converter={StaticResource FailConverter}}" >
                                                    <DataTrigger.Value>True</DataTrigger.Value>
                                                    <Setter Property="Foreground" Value="#ef6eab"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </TextBlock.Style>
                                </TextBlock>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
    

    Method 2

    You could also use a single MultiValueConverter and pass either "Pass" or "Fail" strings with the permission value and check like so:

    <GridViewColumn Header="Permission" Width="170" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Permission}">
                                    <TextBlock.Style>
                                        <Style TargetType="{x:Type TextBlock}">
                                            <Style.Triggers>
                                                <DataTrigger>
                                                    <DataTrigger.Binding>
                                                        <MultiBinding Converter="{StaticResource PassFailConverter}">
                                                            <Binding Path="permission"/>
                                                            <Binding>
                                                                <Binding.Source>
                                                                    <system:String>
                                                                        Pass
                                                                    </system:String>
                                                                </Binding.Source>
                                                            </Binding>
                                                        </MultiBinding>
                                                    </DataTrigger.Binding>
                                                    <DataTrigger.Value>True</DataTrigger.Value>
                                                    <Setter Property="Foreground" Value="#4c72cc"/>
                                                </DataTrigger>
                                                <DataTrigger>
                                                    <DataTrigger.Binding>
                                                        <MultiBinding Converter="{StaticResource PassFailConverter}">
                                                            <Binding Path="permission"/>
                                                            <Binding>
                                                                <Binding.Source>
                                                                    <system:String>
                                                                        Fail
                                                                    </system:String>
                                                                </Binding.Source>
                                                            </Binding>
                                                        </MultiBinding>
                                                    </DataTrigger.Binding>
                                                    <DataTrigger.Value>True</DataTrigger.Value>
                                                    <Setter Property="Foreground" Value="#ef6eab"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </TextBlock.Style>
                                </TextBlock>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
    

    And the multivalueconverter should look something like that:

     public class PassFailConverter:IMultiValueConverter {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            return (values[0] as string).Contains(values[1] as string);
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    Don't forget to include the converter in your static resources:

    <YourNameSpace:PassFailConverter x:Key="PassFailConverter"></local:PassFailConverter>