簡體   English   中英

如何在Silverlight中在運行時基於文本塊中的值更新邊框的背景色?

[英]How to update background color of the border based on value in textblock in runtime in Silverlight?

我有一個帶有兩個元素的DataTemplate。 我可以在運行時更新textblock的值。 我需要根據texblock的值更新邊框背景。 例如,當texblock獲得“否”值時,我需要將邊框背景設置為紅色,並在texblock獲得字符串值“是”時將顏色更改為綠色。 我應用了TwoWay綁定,但是它僅更新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" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM