Silverlight / WPF中根据绑定数据的值切换数据模板
假设我正在使用 WPF 或 Silverlight 并将 ContentPresenter 绑定到整数属性:
<ContentPresenter Content={Binding Score} />
如果分数为 10,我想显示一颗金星,否则只显示数字。所以本质上我有两个可能的数据模板:
<Path Fill="Gold" Data="..." />
<TextBlock Text="{Binding Score}" />
设置它的最佳方法是什么?是使用绑定转换器吗?或者绑定到动态加载适当的数据模板 xaml 并根据 Score 的值生成正确的 FrameworkElement 的不同对象?或者我还缺少另一个技巧 - 也许 ContentPresenter 不是正确使用的控件?
我想知道您是否可以执行类似的操作,但它不喜欢 ContentTemplate 值中的嵌套绑定:
<StackPanel>
<StackPanel.Resources>
<DataTemplate x:Key="LowScore">
<TextBlock Text="{Binding Path=Score}" Foreground="Red" />
</DataTemplate>
<DataTemplate x:Key="HighScore">
<Path Fill="Gold" Data="M 0,0 l 10,0 l 5,-10 l 5,10 l 10,0 l -7,10 l 2,10 l -10,-5 l -10,5 l 2,-10 Z" />
</DataTemplate>
</StackPanel.Resources>
<ContentPresenter Content="{Binding Score}" ContentTemplate="{StaticResource ResourceKey={Binding ScoreTemplate}}">
</ContentPresenter>
</StackPanel>
say I am using WPF or Silverlight and binding a ContentPresenter to an integer property:
<ContentPresenter Content={Binding Score} />
and if the score is 10 I want to display a gold star, and otherwise just display the number. So essentially I have two possible data templates:
<Path Fill="Gold" Data="..." />
<TextBlock Text="{Binding Score}" />
What is the best way to set this up? Is it to use a Binding Converter? Or bind to a different object that dynamically loads the appropriate data template xaml and makes the correct FrameworkElement depending on the value of Score? Or is there another trick I am missing - perhaps ContentPresenter isn't the right control to be using?
I wondered if you could do something like this, but it doesn't like the nested binding within the ContentTemplate value:
<StackPanel>
<StackPanel.Resources>
<DataTemplate x:Key="LowScore">
<TextBlock Text="{Binding Path=Score}" Foreground="Red" />
</DataTemplate>
<DataTemplate x:Key="HighScore">
<Path Fill="Gold" Data="M 0,0 l 10,0 l 5,-10 l 5,10 l 10,0 l -7,10 l 2,10 l -10,-5 l -10,5 l 2,-10 Z" />
</DataTemplate>
</StackPanel.Resources>
<ContentPresenter Content="{Binding Score}" ContentTemplate="{StaticResource ResourceKey={Binding ScoreTemplate}}">
</ContentPresenter>
</StackPanel>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用模板选择器。 这是一个关于打开代码的很好的教程。基本上,模板选择器允许您根据您想要的任何条件选择项目的模板。
you could use a template selector. Here is a nice tutorial on Switch On The Code. Basically, a template selector allows you to select the template for an item based on whatever conditions you want.
可能的解决方案:
创建一个包含两种控件类型的 StackPanel 的 DataTemplate,并绑定它们的可见性(或使用 DataTrigger),以便一次只有一个可见。这相当简单,如果状态不多或差异很小,这会很好。
使用 DataTemplateSelector 并按资源查找 DataTemplate。
Possible solutions:
Create a DataTemplate with a StackPanel containing both control types and bind their Visibility (or use a DataTrigger) so that only one is visible at a time. This is fairly simple and can be good if there are not many states or the differences are small.
Use a DataTemplateSelector and look up the DataTemplate by resource.