uwp-xaml - 如何将字符串与 UWP 中 DataTemplate 内按钮中的绑定(bind)属性内容连接起来?

标签 uwp-xaml

<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&#160;"/>
            </TextBlock>
            <TextBlock Text="{Binding ItemsCount}" />
        </StackPanel>
    </Button>

关于uwp-xaml - 如何将字符串与 UWP 中 DataTemplate 内按钮中的绑定(bind)属性内容连接起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37087518/

相关文章:

xaml - UWP Light 关闭 ContentDialog

c++ - 在 Windows 10 上合成语音 : Scenario_SynthesizeText. xaml.h 丢失

winrt-xaml - XAML 样式的 resx 资源的引用字符串

c# - 我得到 'A project cannot have more than one ApplicationXaml item'

uwp - broadFileSystemAccess UWP

uwp - 我找不到任何对 UWP Xbox 字体的引用

c# - UWP 应用程序使用绑定(bind)显示/隐藏按钮

visual-studio-2015 - 当我将我的应用程序与商店关联时,Visual Studio 崩溃

c# - UWP 应用中的表

xaml - 未设置 UWP PersonPicture 控件背景