簡體   English   中英

WP7:根據另一個Textblock的值更改Textblock前景顏色

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

說我有這個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)我需要在轉換器中更改Textblock b3的前景色的代碼是什么? 並且(2),我是否在Textblock“ b3”的數據上下文中正確調用了轉換器? 謝謝!

如果您的文本塊b1已綁定到變量(此處為team1),您還可以使用Converter將t3的Foreground綁定到它:

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

在您的轉換器中,tema1的值將作為值(對象)傳遞:

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

   }

}    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM