如何将 StringFormat 添加到 DataTemplate 内的文本块?
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有一种直接的机制可以让一个 DataTemplate 继承另一个 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: