根据数据源的另一个字段有条件地设置 XAML 中绑定元素的颜色
我的 WP7 列表框中有以下项目模板。我的绑定类型有一个布尔属性(状态),我想根据此有条件地设置站点名称的颜色。请问我该怎么做呢?
提前致谢!
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432">
<TextBlock Text="{Binding SiteName}" TextWrapping="Wrap" />
<TextBlock Text="{Binding Url}" TextWrapping="Wrap" Margin="12,-6,12,0" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
I have the following item template in a WP7 listbox. My bound type has a boolean property (Status) and I would like to conditionally set the colour of the site name based on this. How would I go about doing this please?
Thanks in advance!
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432">
<TextBlock Text="{Binding SiteName}" TextWrapping="Wrap" />
<TextBlock Text="{Binding Url}" TextWrapping="Wrap" Margin="12,-6,12,0" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是使用转换器。您可以让转换器返回一种颜色,或者您可以有两个 TextBlock,每个 TextBlock 都有您想要的颜色,并使用布尔值来显示可见性,以及一个反向可见性转换器来隐藏/显示每个控件。
需要注意的是,转换器可能会很慢,因此有时绑定到 ViewModel 并在该 ViewModel 上提供您需要的转换值会更高效。
The easiest way to do this is with a converter. Either you can have the converter return a color, or you can have two TextBlocks each with the color you want, and use a Boolean to Visibility, and one that is a reverse visibility, converters to hide/show each of the controls.
One word of caution is that converters can be slow, so sometimes it's more performant to bind to a ViewModel and on that ViewModel provide the convetered values that you need.