WP7:根据另一个文本块的值更改文本块前景色
假设我有这个 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 的前景色。我知道我需要实现一个类似于下面的转换器:
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)转换器中需要什么代码来更改文本块 b3 的前景色? (2),我是否在文本块“b3”的数据上下文中正确调用转换器? 谢谢你!
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>
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();
}
}
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的文本块 b1 已经绑定到变量(此处为 team1),您还可以使用 Converter 将 t3 的 Foreground 绑定到它:
并且在您的转换器中,tema1 的值将作为值(oject)传递:
If your textblock b1 is already binded to a variable (here team1), you can also bind Foreground of t3 to it with a Converter:
and in your converter the value of tema1 will be passed as the value (oject):