2010-04-26 26 views
14

我有一個WPF TextBlock綁定到一個字符串。 如果該字符串爲空,我希望TextBlock以另一種顏色顯示警告消息。如何有條件地格式化WPF TextBlock?

這很容易在代碼中做,我想知道是否有一個優雅的WPF純XAML解決方案嗎? 我已經調查過風格觸發器,但是語法對我來說並不自然。

謝謝!

回答

23

添加一些細節Daniel's (slightly short) answer爲一些需要DataTrigger的東西是不是真的微不足道的(如{x:Null}):

<TextBlock Text="{Binding MyTextProperty}"> 
    <TextBlock.Style> 
     <Style TargetType="{x:Type TextBlock}"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding MyTextProperty}" Value="{x:Null}"> 
        <Setter Property="Text" Value="Hey, the text should not be empty!"/> 
        <Setter Property="Foreground" Value="Red"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </TextBlock.Style> 
</TextBlock> 

BTW:這是否完全從記憶,沒有檢查它在VS或混合 ,所以請原諒是否有錯誤。但是,你應該能夠自己排序。重要的是這個想法。祝你好運!

+0

在問題的上下文中,您可能希望在樣式/觸發器中設置可見性,並在第一行設置文本和顏色。然而,這個答案仍然很好,因爲它說明了更復雜格式的開始。 – apc 2016-10-19 14:37:17

9

您可以使用此轉換器。用IValueConverter簡單創建類。在數據綁定後,使用此轉換器

例如您的XAML

<Window x:Class="WpfApplication4.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:lib="clr-namespace:WpfApplication4" 
    Title="Window1" Height="300" Width="300"> 

<Window.Resources> 
    <lib:TextBlockDataConveter x:Key="DataConverter"/> 
    <lib:TextBlockForegroundConverter x:Key="ColorConverter"/> 
</Window.Resources> 

<Grid> 
    <TextBlock Text="{Binding Path=message, Converter ={StaticResource DataConverter}}" Foreground="{Binding message, Converter={StaticResource ColorConverter}}"/> 
</Grid> 

和你的轉換器:

public class TextBlockDataConveter:IValueConverter 
{ 
    #region IValueConverter Members 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value == null) 
     { 
      return "Error Message"; 
     } 
     else 
     { 
      return value; 
     } 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 

    #endregion 
} 

class TextBlockForegroundConverter:IValueConverter 
{ 

    #region IValueConverter Members 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value == null) 
     { 

      SolidColorBrush brush = new SolidColorBrush(); 
      brush.Color = Colors.Red; 
      return brush; 
     } 
     else 
     { 
      SolidColorBrush brush = new SolidColorBrush(); 
      brush.Color = Colors.Black; 
      return brush; 

     } 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 

    #endregion 
} 

它的工作原理。覈實。

+1

難道你不覺得這麼簡單的任務有點太多嗎?! – gehho 2010-04-27 15:45:03

+1

是的代碼太多了。您也可以使用DataTriggers完成您的任務。但是這個解決方案更加靈活。 – Polaris 2010-04-29 05:34:08

+0

嗨北極星,謝謝,但我正在專門尋找一個Xaml唯一的解決方案。 – 2010-05-03 23:57:41