开发者

WP7: Change Textblock Foreground color based on another Textblock's value

开发者 https://www.devze.com 2023-01-31 01:55 出处:网络
Say I have this XAML: <TextBlock Name=\"t1\" Text=\"{Binding team1}\" Foreground=\"White\"FontSize=\"32\"/>

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 开发者_C百科b3? And (2), am I calling the converter correctly in the datacontext of Textblock "b3"? Thank you!


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

   }

}    
0

精彩评论

暂无评论...
验证码 换一张
取 消