WP7:根据另一个文本块的值更改文本块前景色

发布于 2024-10-08 21:08:44 字数 2200 浏览 0 评论 0原文

假设我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱本泡沫多脆弱 2024-10-15 21:08:44

如果您的文本块 b1 已经绑定到变量(此处为 team1),您还可以使用 Converter 将 t3 的 Foreground 绑定到它:

Foreground="{Binding team1, Converter={StaticResource YourConverter}}"

并且在您的转换器中,tema1 的值将作为值(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...

   }

}    

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

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...

   }

}    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文