0
votes

I have a DataTemplate with two elements. I can update textblock value on runtime. I need to update border background based on the value from texblock. For example, I need to make border background in red when texblock gets “No” value and change color to green where texblock gets string value “Yes”. I applied TwoWay binding but it only updates the value of the texblock with no effect to the border background color. Any advice is highly appreciated! Below is the XAML:

<UserControl.Resources>
    <DataTemplate x:Key="DataTemplateYesNo">
        <StackPanel Orientation="Horizontal">
            <Border x:Name="BoxColor" Width="10" Height="10" VerticalAlignment="Center" Background="#FF00FF3E" Margin="0,0,5,0" >
                <i:Interaction.Triggers>
                    <ic:DataTrigger Binding="{Binding Y}" Value="No">
                        <ic:ChangePropertyAction PropertyName="Background" Duration="0">
                            <ic:ChangePropertyAction.Value>
                                <SolidColorBrush Color="Red"/>
                            </ic:ChangePropertyAction.Value>
                        </ic:ChangePropertyAction>
                    </ic:DataTrigger>
                </i:Interaction.Triggers>
            </Border>
            <TextBlock Text="{Binding Y, Mode=TwoWay}" VerticalAlignment="Center" />
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>

<StackPanel Orientation="Horizontal">
   <data:DataGrid x:Name="mdg" ItemsSource="{Binding Coordinates, Mode=TwoWay}"
                   AutoGenerateColumns="False">
        <data:DataGrid.Columns>             
            <data:DataGridTextColumn Header="X Position" Width="100" Binding="{Binding X, Mode=TwoWay}"/>
            <data:DataGridTemplateColumn Header="Y Position" Width="100" CellTemplate="{StaticResource DataTemplateYesNo}" />
        </data:DataGrid.Columns>
    </data:DataGrid>
</StackPanel>
1
I do not see neither Yes nor No mentioned in your code, how did you expect to do it? - Snowbear
It was Typo. I just fixed it. It has to be "No" in order to be red. - vladc77

1 Answers

0
votes

Binding converter:

class YesNoStringToColorConverter : IValueConverter
{
    public object Convert(object value, ...)
    {
        if (value == "Yes") return new SolidColorBrush(Colors.Green);
        if (value == "No") return new SolidColorBrush(Colors.Red);
        return null;
    }

    ...
}

XAML

        <Border BorderBrush="{Binding Text, ElementName="textBlock", Converter=/*Pass YesNoStringToColorConverter here*/}" ...>
        <TextBlock Text="{Binding Y}" x:Name="textblock" />