【发布时间】:2011-10-15 19:49:15
【问题描述】:
我想知道是否可以使用以下类型的绑定,但我无法形成一个搜索查询来给我明确的答案,或者在任何地方找到它。
就是这样。
我有一个带有“项目”(自定义类)的列表框,这些项目使用项目模板显示。我使用转换器来计算背景颜色。见下文:
<Window.Resources>
<local:MyConverter x:Key="myConverter"/>
</Window.Resources>
<!-- snip -->
<ListBox Grid.Row="2" Name="listBoxMaterialType" Margin="0,0,0,0" ItemsSource="{Binding Path=MyItems, ElementName=MySource}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding DisplayText}" Background="{Binding Converter={StaticResource myConverter}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
现在为了更好的抽象,我希望 MyConverter 不转换项目,而只是转换项目的分数。一个项目不知道它自己的分数,但分数是通过调用 MyStaticClass.MyStaticMethod(item); 计算出来的。所以在代码中我想做这样的绑定: myTextBlock.Background= MyConverter(MyStaticClass.MyStaticMethod(item));是否可以通过数据绑定在 WPF 中做到这一点?
【问题讨论】:
标签: c# wpf xaml data-binding converter