【问题标题】:WPF DataTemplate method binding with parameterWPF DataTemplate 方法与参数绑定
【发布时间】:2012-06-21 15:31:06
【问题描述】:

我有一个用于 ListBox 的自定义 ItemTemplate,我需要将 TextBlock“绑定”到一些特殊的方法/属性。

我的列表框源是ObservableCollection<SearchResultItem>SearchResultItem 包含一些属性。

文本需要根据另一个对象的值进行更改。例如,如果此对象等于“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 方法。

-假设“foo”值来自ConfigurationManager.AppSettings["propertyName"];,如果有帮助的话。

【问题讨论】:

  • 你试过用转换器吗?
  • 我见过一些使用转换器的代码示例,但我不知道如何以这种方式访问​​“foo”对象值。
  • 你说你有一个用于自定义 ListBox 的 DataTemplate,但我想你想说的是你有一个用于 ListBox 的自定义 ItemTemplate ......我说得对吗?
  • @Ryan 你是对的,我编辑了 OP
  • @DannyWillem,好的,一旦 SearchResultItem 添加到集合中,属性的值会改变吗?

标签: c# wpf binding datatemplate


【解决方案1】:

只需使用带有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"/>

【讨论】:

    【解决方案2】:

    好的,因为您的属性永远不会改变,这是一种解决方案。您可以通过 SearchResultItem 类的另一个属性来执行此操作:

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

    然后将你的文本框绑定到这个属性:

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

    【讨论】:

    • 我不能这样做,因为 SearchResultItem 来自外部库,它不知道“foo”。
    • 您可以为它编写一个包含您的 SearchResultItem 的包装器,并将您的 ObservableCollection 更改为这些包装器的集合
    • 我在想这个,但我不太喜欢包装的想法。我正在考虑绑定/ xaml 解决方案。事实上,如果没有提出其他解决方案,我会继续包装我的项目。
    • 但是您仍然会绑定...只是绑定到一个包装类而不是您的 SearchResultItem。我不确定您的想法是哪种解决方案,但由于 SearchResultItem 仅公开 GetProperty 方法,因此您需要一些包装器,以便您可以从代码中调用该方法并在绑定属性中返回该值
    • 我终于选择了包装!谢谢
    猜你喜欢
    • 2017-08-17
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多