我已经学会了如何在标签的content属性中格式化字符串,如下所示:
<Label Content="{Binding ElementName=theSlider, Path=Value}"
ContentStringFormat="The font size is {0}."/>
我想在Setter中做同样的事情但是“ValueStringFormat”不存在,在这里做我想做的正确语法是什么:
<DataTrigger Binding="{Binding Path=Kind}" Value="Task">
<Setter TargetName="TheTitle" Property="Text"
Value="{Binding Title}"
ValueStringFormat="Your title was: {0}"/>
</DataTrigger>
答案 0 :(得分:5)
你能简单地使用Binding本身的StringFormat属性吗?
<DataTrigger Binding="{Binding Path=Kind}" Value="Task">
<Setter TargetName="TheTitle" Property="Text"
Value="{Binding Title,StringFormat='Your title was: {}{0}'}"
/>
</DataTrigger>
答案 1 :(得分:1)
我无法测试,但希望它有效:
...
xmlns:local="clr-namespace:ValueConverter"
...
<Window.Resources>
<local:MyTextConverter x:Key="MyTextConverter" />
</Window.Resources>
...
<DataTrigger Value="Task">
<DataTrigger.Binding>
<Binding Converter="{StaticResource MyTextConverter}"
Path="Kind" />
</DataTrigger.Binding>
<Setter TargetName="TheTitle" Property="Text"/>
</DataTrigger>
其中MyTextConverter是一个实现IValueConverter接口的类:
public class PositionConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture)
{
return String.Format("Your title was: {0}", value);
}
public object ConvertBack(object value, Type targetType, object parameter,
CultureInfo culture)
{
throw new Exception("The method or operation is not implemented.");
}