WP7:根据另一个文本块的值更改文本块前景色 [英] WP7: Change Textblock Foreground color based on another Textblock's value
问题描述
假设我有这个 XAML:
Say I have this XAML:
<TextBlock Name="t1" Text="{Binding team1}" Foreground="White" FontSize="32" />
<ListBox Name="lbBooks" Width="441" Height="490" >
<ListBox.ItemTemplate>
<DataTemplate x:Name="d1" >
<StackPanel Name="spMain">
<StackPanel Orientation="Horizontal" >
<HyperlinkButton Content="{Binding BookName}" Margin="5" Width="230" TargetName="_blank" NavigateUri="{Binding BookWebsite}"/>
<StackPanel Orientation="Vertical" HorizontalAlignment="Right" Margin="0,0,0,0" >
<TextBlock Name="b1" Text="{Binding BookLine1}" Margin="5" Width="160" HorizontalAlignment="Right"></TextBlock>
<TextBlock Name="b1U" Text="{Binding BookLine2}" Margin="5" Width="160" Foreground="Wheat" HorizontalAlignment="Right"></TextBlock>
<TextBlock Name="b3" Text="{Binding BookLine3}" Margin="5" Width="160" DataContext="{Binding team1,Converter={StaticResource tbConverter}, ElementName=b3, Mode=TwoWay}" HorizontalAlignment="Right"></TextBlock>
</StackPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我想根据 TextBlock "t1" 的值更改名为 "b3" 的 TextBlock 的前景色.我知道我需要实现一种如下所示的转换器:
I want to change the foreground color of the TextBlock named "b3" depending on the value of TextBlock "t1". I know I need to implement a converter kind of like the one below:
public class TBConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//do I need to check against the Textblock t1 value in here?
if (value != null && t1.Text == "Text that triggers change" )
{
//need code to change Textblock foreground color
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
那么,(1) 我需要在转换器中更改 Textblock b3 的前景色的代码是什么?(2),我是否在 Textblockb3"的数据上下文中正确调用了转换器?谢谢!
So,(1) what is the code I need in the converter to change the foreground color of the Textblock b3? And (2), am I calling the converter correctly in the datacontext of Textblock "b3"? Thank you!
推荐答案
如果你的 textblock b1 已经绑定了一个变量(这里是 team1),你也可以使用 Converter 绑定 t3 的 Foreground 到它:
If your textblock b1 is already binded to a variable (here team1), you can also bind Foreground of t3 to it with a Converter:
Foreground="{Binding team1, Converter={StaticResource YourConverter}}"
并且在您的转换器中,tema1 的值将作为值(对象)传递:
and in your converter the value of tema1 will be passed as the value (oject):
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var team1 = valus as YourType
if(team1 == xxx)
{
return newColorBrush;
}else{
return defaultColorBrush; //maybe from your styles etc...
}
}
这篇关于WP7:根据另一个文本块的值更改文本块前景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!