<DataTemplate x:Key="SectionsTemplate">
<Grid Background="LightSkyBlue">
<Button Content="{Binding ItemsCount}"></Button>
</Grid>
</DataTemplate>
我想在按钮内容中添加带有 {Binding ItemsCount} 的“Items”字符串。
最佳答案
您应该为此使用转换器:
将新转换器添加到您的项目
public class PrependStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return (string)parameter + " " + (string)value;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
// implement for two-way convertion
throw new NotImplementedException();
}
}
在资源中添加对转换器的引用
<Page.Resources>
<local:PrependStringConverter x:Key="PrependStringConverter" />
</Page.Resources>
在数据绑定(bind)中使用转换器
<Button Content="{Binding ItemsCount, ConverterParameter=Items, Converter={StaticResource PrependStringConverter}}">
</Button>
还有另一种选择:使用 StackPanel 等分组元素将按钮的内容分成两部分:
<Button >
<StackPanel Orientation="Horizontal">
<TextBlock>
<Run Text="Items "/>
</TextBlock>
<TextBlock Text="{Binding ItemsCount}" />
</StackPanel>
</Button>
关于uwp-xaml - 如何将字符串与 UWP 中 DataTemplate 内按钮中的绑定(bind)属性内容连接起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37087518/