我有一个ListBox的自定义ItemTemplate,我需要将一个TextBlock绑定到某些特殊的方法/属性。WPF DataTemplate方法绑定参数
我的列表框源是一个ObservableCollection<SearchResultItem>
。 SearchResultItem
包含一些属性。
该文本需要根据另一个对象的值进行更改。 E.G如果此对象等于“foo”,则需要使用文本值调用SearchResultItem
上的方法GetProperty("foo")
以获取正确的值。
这里是一个代码示例:
<DataTemplate>
..
//Here is a Label bound to the Date Property of the SearchResultItem
<Label Margin="2,2,2,0" Grid.Row="0" Grid.Column="2" Content="{Binding Path=Date}" HorizontalAlignment="Right" HorizontalContentAlignment="Right" />
//Here is the textblock that needs to call the method with the parameter based on the value of the other object.
<TextBlock Margin="2,2,2,0" TextTrimming="CharacterEllipsis" Grid.Row="0" Grid.Column="1" Text="I need some help there" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black"/>
..
</DataTemplate>
你有关于如何做到这一点的任何想法或更好的方式来做到这一点?
编辑:
- 让我们假设SearchResultItem
来自外部库,只露出GetProperty
方法。
- 这就是说,如果有帮助,“富”值来自ConfigurationManager.AppSettings["propertyName"];
。
您是否尝试过使用转换器? – benjer3
我见过一些使用转换器的代码示例,但我不知道如何通过这种方式访问“foo”对象的值。 –
你说你有一个自定义列表框的DataTemplate,但我想你想说的是,你有一个ListBox的自定义ItemTemplate ...我是否正确? – Thelonias