2013-07-04 3 views
0

나는 Price이라는 TextBlock입니다. 작동하는 DataTrigger 있습니다.WPF 텍스트 블록 텍스트 및 DataTrigger

<DataTrigger Binding="{common:ComparisonBinding DataContext.Discount,GT,0}" Value="{x:Null}"> 
    <DataTrigger.Setters> 
     <Setter Property="Text" TargetName="price"> 
      <Setter.Value> 
       <Run>Value1</Run> 
       <Run>Value2</Run> 
      </Setter.Value>     
     </Setter> 
    </DataTrigger.Setters> 
</DataTrigger> 

그래서이 Discount is > 0 경우이 나던 작품 내부 Text.However으로 실행이 표시되어야 것을 의미한다. 다른 텍스트 스타일이 필요하기 때문에 바인딩이 필요합니다.

답변

2

xaml 및 @BasBrekelmans의 오류로 인해 Run 요소를 string이 필요한 속성에 할당하려고했습니다.

귀하의 요구 사항에 맞게 StringFormat과 함께 을 사용하여 바운드 값을 필수 형식으로 포맷하십시오.

뭔가 같은 :

<DataTrigger Binding="{common:ComparisonBinding DataContext.Discount,GT,0}" 
       Value="{x:Null}"> 
    <Setter TargetName="price" 
      Property="Text"> 
     <Setter.Value> 
     <MultiBinding StringFormat="Some Custom Formatted Text Value1: {0} and Value2: {1}"> 
      <Binding Path="BindingValue1" /> 
      <Binding Path="BindingValue2" /> 
     </MultiBinding> 
     </Setter.Value> 
    </Setter> 
</DataTrigger> 

TextBlock 당신이 하나의 TextBlock보다 더 나은 요소와 컨트롤의 템플릿을 수정하는 것이 더 낫다 인라인의 바인딩 예치으로 조정할하려는의 시각적 스타일이 있다면 그것을 허용하십시오. 당신이 변환기를 사용하고 적용하여 작품을 주위에 사용할 수 그러나

DataTrigger.SetterTextBlock.Tag

말 뭔가 같은 :

public class TextBlockInlineFormatConverter : IMultiValueConverter { 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { 
    if (values.Length < 3) 
     return null; 
    TextBlock textblock = values[0] as TextBlock; 
    if (textblock == null) 
     return null; 
    textblock.ClearValue(TextBlock.TextProperty); 
    textblock.Inlines.Add(new Run("Some text ") { Foreground = Brushes.Tomato }); 
    textblock.Inlines.Add(new Run(values[1].ToString()) { Foreground = Brushes.Blue }); 
    textblock.Inlines.Add(new Run(" and Some other text ") { Foreground = Brushes.Tomato }); 
    textblock.Inlines.Add(new Run(values[2].ToString()) { Foreground = Brushes.Blue, FontWeight = FontWeights.Bold }); 
    return textblock.Tag; 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { 
    throw new NotImplementedException(); 
    } 
} 

및 사용 :

<DataTrigger Binding="{common:ComparisonBinding DataContext.Discount,GT,0}" 
       Value="{x:Null}"> 
    <!-- Note the setter is on Tag and not Text since we modify the Text using Inlines within the converter --> 
    <Setter TargetName="price" 
      Property="Tag"> 
     <Setter.Value> 
     <MultiBinding Converter="{StaticResource TextBlockInlineFormatConverter}" 
         Mode="OneWay"> 
      <Binding Path="." 
        RelativeSource="{RelativeSource Self}" /> 
      <Binding Path="BindingValue1" /> 
      <Binding Path="BindingValue2" /> 
     </MultiBinding> 
     </Setter.Value> 
    </Setter> 
</DataTrigger> 

이를 사용 제어 템플릿 tbh를 수정하는 것이 제한되어있는 경우에만 해결하십시오.

+0

정말 멋 있었어요 :) 고마워요.하지만 시간 압력으로 인해 빈 텍스트가있는 두 번째 텍스트 블록을 추가하고 트리거로 텍스트를 바꿉니다. 어떤 것이 더 효율적인지는 확실하지 않습니다. – GorillaApe

1

Run 컬렉션은 문자열 인 Text 속성에 적용 할 수 없습니다. 올바른 속성은 Inlines입니다.

불행히도이 속성에는 설정자가 없으므로이를 해결할 수있는 다른 방법이 있어야합니다. 예 : ContentControlStackPanel에 2 개의 TextBlock을 갖는다.