如何在Silverlight运行时根据textblock中的值更新边框的背景色?

如何在Silverlight运行时根据textblock中的值更新边框的背景色?,silverlight,data-binding,datagrid,datatemplate,Silverlight,Data Binding,Datagrid,Datatemplate,我有一个包含两个元素的DataTemplate。我可以在运行时更新textblock值。我需要根据texblock的值更新边框背景。例如,当texblock获得“否”值时,我需要将边框背景设置为红色,当texblock获得字符串值“是”时,我需要将颜色更改为绿色。 我应用了双向绑定,但它只更新texblock的值,对边框背景色没有影响。非常感谢您的建议! 以下是XAML: <UserControl.Resources> <DataTemplate x:Key="Data

我有一个包含两个元素的DataTemplate。我可以在运行时更新textblock值。我需要根据texblock的值更新边框背景。例如,当texblock获得“否”值时,我需要将边框背景设置为红色,当texblock获得字符串值“是”时,我需要将颜色更改为绿色。 我应用了双向绑定,但它只更新texblock的值,对边框背景色没有影响。非常感谢您的建议! 以下是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>

绑定转换器:

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" />