简体   繁体   English

WPF DataTemplate方法与参数绑定

[英]WPF DataTemplate method binding with parameter

I have a custom ItemTemplate for a ListBox and I need to "bind" a TextBlock to some special method / property. 我有一个用于ListBox的自定义ItemTemplate,我需要将TextBlock“绑定”到一些特殊的方法/属性。

My ListBox Source is an ObservableCollection<SearchResultItem> . 我的列表框源是ObservableCollection<SearchResultItem> SearchResultItem containing some properties. SearchResultItem包含一些属性。

The text need to change based on the value of another object. 文本需要根据另一个对象的值进行更改。 EG if this object equals "foo" I need the text value to call the method GetProperty("foo") on the SearchResultItem to get the correct value. 例如,如果此对象等于“ foo”,则我需要文本值来调用SearchResultItem上的方法GetProperty("foo")以获取正确的值。

Here is a sample of code: 这是代码示例:

<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>

Do you have any idea on how to do that or a better way to do it? 您对如何执行此操作或更好的方法有任何想法吗?

Edit: 编辑:

-Let's assume SearchResultItem comes from an external library and only exposes the GetProperty method. -假设SearchResultItem来自外部库,并且仅公开GetProperty方法。

-Let's say the "foo" value comes from ConfigurationManager.AppSettings["propertyName"]; -假设“ foo”值来自ConfigurationManager.AppSettings["propertyName"]; if it helps. 如果有帮助。

OK, because your properties never change, here's one solution. 好的,因为您的属性永远不变,所以这是一种解决方案。 You can do this via another property on your SearchResultItem class: 您可以通过SearchResultItem类的另一个属性来执行此操作:

public string MyTextBinding
{
    get 
    {
        return myDictionary.ContainsKey("foo") ? return myDictionary["foo"] : return "myDictionary doesn't contain foo"; 
    }
}

Then just bind your textbox to this property: 然后将文本框绑定到此属性:

<DataTemplate>    
    <Label Margin="2,2,2,0" Grid.Row="0" Grid.Column="2" Content="{Binding Path=Date}" HorizontalAlignment="Right" HorizontalContentAlignment="Right" />
    <TextBlock Margin="2,2,2,0" TextTrimming="CharacterEllipsis"  Grid.Row="0" Grid.Column="1" Text="{Binding Path=MyTextBinding, Mode=OneWay}" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black"/>
</DataTemplate>

Just use a IValueConverter that takes a SearchResultItem and return the expected text 只需使用带有SearchResultItemIValueConverter并返回期望的文本

<TextBlock Margin="2,2,2,0" TextTrimming="CharacterEllipsis" 
           Grid.Row="0" Grid.Column="1" 
           Text="{Binding Path=., 
                Converter={StaticResource PropertyValueFromSearchResultItemConverter}, 
                ConverterParameter=Foo}" 
           HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM