正如xaml和@BasBrekelmans中的錯誤所述,您嘗試將Run
元素分配給期望值爲string
的屬性。
根據您的要求,只需使用MultiBinding
和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