2010-04-26 28 views
14

Tôi có một khung văn bản WPF bị ràng buộc vào một chuỗi. Nếu chuỗi đó trống, tôi muốn TextBlock hiển thị thông báo cảnh báo bằng màu khác.Làm cách nào để định dạng có điều kiện một TextFlock WPF?

Điều này rất dễ làm trong mã, tôi đã tự hỏi nếu có một giải pháp XAML tinh khiết WPF thanh lịch cho nó? Tôi đã điều tra Trình kích hoạt kiểu, nhưng cú pháp không tự nhiên đối với tôi.

Cảm ơn!

Trả lời

23

Thêm một số chi tiết để Daniel's (slightly short) answer như một số các thứ DataTrigger cần thiết là không thực sự tầm thường (như {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: Phương pháp này có hoàn toàn từ bộ nhớ, đã không kiểm tra nó trong VS hoặc Blend , vì vậy xin vui lòng xin lỗi nếu có lỗi trong đó. Tuy nhiên, bạn có thể tự mình sắp xếp chúng. Cái đếm là ý tưởng. Chúc may mắn!

+0

Trong ngữ cảnh của câu hỏi, bạn có thể muốn đặt chế độ hiển thị trong kiểu/trình kích hoạt và đặt văn bản và màu sắc ở dòng đầu tiên. Tuy nhiên câu trả lời này vẫn tốt vì nó minh họa cho sự bắt đầu của định dạng phức tạp hơn. – apc

9

Bạn có thể sử dụng Trình chuyển đổi cho việc này. Đơn giản chỉ cần tạo lớp với IValueConverter. Sau khi ở databinding sử dụng chuyển đổi

này Ví dụ XAML của bạn

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

và bộ chuyển đổi của bạn:

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 
} 

nó hoạt động. Kiểm tra nó.

+1

Bạn không cảm thấy nó có quá nhiều mã cho một nhiệm vụ đơn giản như vậy ?! – gehho

+1

Có quá nhiều mã. Bạn cũng có thể thực hiện nhiệm vụ của mình với DataTriggers. Nhưng giải pháp này linh hoạt hơn. – Polaris

+0

Hi Polaris, cảm ơn, nhưng tôi đã tìm kiếm một giải pháp duy nhất cho Xaml. –

Các vấn đề liên quan