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.Setter
에 TextBlock.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를 수정하는 것이 제한되어있는 경우에만 해결하십시오.
출처
2013-07-04 13:44:54
Viv
정말 멋 있었어요 :) 고마워요.하지만 시간 압력으로 인해 빈 텍스트가있는 두 번째 텍스트 블록을 추가하고 트리거로 텍스트를 바꿉니다. 어떤 것이 더 효율적인지는 확실하지 않습니다. – GorillaApe