如何将 StringFormat 添加到 DataTemplate 内的文本块?

发布于 2024-08-09 09:44:31 字数 1297 浏览 4 评论 0原文

我有以下 DataTemplate

<DataTemplate x:Key="ColoringLabels">
    <TextBlock Padding="0"
               Margin="0"
               Name="Username"
               Text="{Binding Username}"
               Foreground="Gray"
               FontStyle="Italic"
              />
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding IsLoggedIn}" Value="True">
            <Setter TargetName="Username" Property="FontSize" Value="14"/>
            <Setter TargetName="Username" Property="Foreground" Value="Green"/>
            <Setter TargetName="Username" Property="FontStyle" Value="Normal"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

我想在 ListView 中使用该模板,其中每个用户名后面都跟着一个 ;和一个空间。

实际上,模板的行为就像这样编写的:

<DataTemplate x:Key="ColoringLabels">
    <TextBlock Padding="0"
               Margin="0"
               Name="Username"
               Text="{Binding Username, StringFormat='{}{0}; '}"
               Foreground="Gray"
               FontStyle="Italic"
              />
    <DataTemplate.Triggers>
      ...
    </DataTemplate.Triggers>
</DataTemplate>

如何扩展原始模板以获得第二个模板的结果?

I have the following DataTemplate:

<DataTemplate x:Key="ColoringLabels">
    <TextBlock Padding="0"
               Margin="0"
               Name="Username"
               Text="{Binding Username}"
               Foreground="Gray"
               FontStyle="Italic"
              />
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding IsLoggedIn}" Value="True">
            <Setter TargetName="Username" Property="FontSize" Value="14"/>
            <Setter TargetName="Username" Property="Foreground" Value="Green"/>
            <Setter TargetName="Username" Property="FontStyle" Value="Normal"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

I would like to use the template in a ListView where every username is followed by a ; and a space.

Effectively the template would then behave as it were written like this:

<DataTemplate x:Key="ColoringLabels">
    <TextBlock Padding="0"
               Margin="0"
               Name="Username"
               Text="{Binding Username, StringFormat='{}{0}; '}"
               Foreground="Gray"
               FontStyle="Italic"
              />
    <DataTemplate.Triggers>
      ...
    </DataTemplate.Triggers>
</DataTemplate>

How can I extend the original template to get the result of the second one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪眸﹌ 2024-08-16 09:44:31

没有一种直接的机制可以让一个 DataTemplate 继承另一个 DataTemplate 的属性。

但是,您可以通过使用具有继承功能的样式成功地避免代码重复。

我相信这会给你你所需要的:

    <Style x:Key="StandardBoundTb" TargetType="TextBlock">
        <Setter Property="Padding" Value="0" />
        <Setter Property="Margin" Value="0" />
        <Setter Property="Text" Value="{Binding Path=Username}" />
        <!-- etc -->
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsLoggedIn}" Value="True">
                <Setter Property="FontSize" Value="14" />
                <!-- etc -->
            </DataTrigger>
        </Style.Triggers>
    </Style>
    <Style x:Key="DelimitedBoundTb" TargetType="TextBlock" 
           BasedOn="{StaticResource StandardBoundTb}"
    >
        <Setter Property="Text" Value="{Binding Path=Username, StringFormat='{}{0}; '}" />
    </Style>

    <DataTemplate x:Key="ColoringLabels">
        <TextBlock Style="{StaticResource StandardBoundTb}" />
    </DataTemplate>
    <DataTemplate x:Key="ColoringLabelsDelimited">
        <TextBlock Style="{StaticResource DelimitedBoundTb}" />
    </DataTemplate>

There isn't a direct mechanism to have one DataTemplate inherit the properties of another one.

However, you can succesfully avoid code duplication by using styles, which DO have inheritance capabilities.

I believe this would give you what you need:

    <Style x:Key="StandardBoundTb" TargetType="TextBlock">
        <Setter Property="Padding" Value="0" />
        <Setter Property="Margin" Value="0" />
        <Setter Property="Text" Value="{Binding Path=Username}" />
        <!-- etc -->
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsLoggedIn}" Value="True">
                <Setter Property="FontSize" Value="14" />
                <!-- etc -->
            </DataTrigger>
        </Style.Triggers>
    </Style>
    <Style x:Key="DelimitedBoundTb" TargetType="TextBlock" 
           BasedOn="{StaticResource StandardBoundTb}"
    >
        <Setter Property="Text" Value="{Binding Path=Username, StringFormat='{}{0}; '}" />
    </Style>

    <DataTemplate x:Key="ColoringLabels">
        <TextBlock Style="{StaticResource StandardBoundTb}" />
    </DataTemplate>
    <DataTemplate x:Key="ColoringLabelsDelimited">
        <TextBlock Style="{StaticResource DelimitedBoundTb}" />
    </DataTemplate>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文