如何在Silverlight运行时根据文本块中的值更新边框的背景颜色?
我有一个包含两个元素的 DataTemplate。我可以在运行时更新文本块值。我需要根据 texblock 的值更新边框背景。例如,当 texblock 获取“No”值时,我需要将边框背景设置为红色,并在 texblock 获取字符串值“Yes”时将颜色更改为绿色。 我应用了 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
绑定转换器:
XAML
Binding converter:
XAML