【问题标题】:ConverterParameter: Setting it inside an ItemTemplateConverterParameter:在 ItemTemplate 中设置它
【发布时间】:2012-02-03 11:40:48
【问题描述】:

我正在尝试将转换器参数设置为 ItemTemplate 中项目的属性。 由于 ConverterParameter 不是依赖属性绑定不起作用。
但是我真的不需要绑定,只需设置一次就足够了,因为它永远不会改变。

<ItemsControl ItemsSource="ItemsWithTypeProperty">
 <ItemsControl.ItemTemplate>
  <DataTemplate>
   <RadioButton IsChecked="{Binding SelectedItem.Base.Type, Converter={l:IsEqualConverter}, Mode=TwoWay, ConverterParameter={Type}}" />
  </DataTemplate>
 </ItemsControl.ItemTemplate>
</ItemsControl>

IsEqualConverter:
转换:将值与参数进行比较并返回结果
ConvertBack:如果值为true则返回参数

ItemTemplate 的 DataContext 是一个包含名为“Type”的属性的类。
它的类型是对象。

有没有办法将 {Type} 替换为只需将其设置为 (DataContext.)Type 一次的东西?如果是怎么办?

【问题讨论】:

    标签: wpf converter itemtemplate


    【解决方案1】:

    我不确定 Type 到底是什么。它是对象的 System.Type 吗?它总是固定类型吗?你可以这样写:

    ConverterParameter={x:Type local:TypeToCompare}
    

    如果 Type 不是常量,您可以重新编写转换器以实现 IMultiValueConverter 并使用 MultiBinding

    【讨论】:

    • No Type 不是常量。在这种情况下,类型是指 DataContext 对象的名称为“类型”的属性。多重绑定也是我的第一个想法,但问题是我无法将其转换回来。 ConvertBack 只提供一个 bool 值,不足以将其转换回我需要的对象。
    • 好吧,在这种情况下,我会说要么回到绘图板,要么如果你真的必须这样做,你有一些技巧可以使 ConverterParameter 可绑定。见marlongrech.wordpress.com/2008/08/03/…
    【解决方案2】:

    尝试使用这个:

    ......, ConverterParameter=Type}" />
    

    在你的转换器上:

        if ((string)parameter == "Type"))
        {
            //Do some stuff
        }
    

    【讨论】:

    • 这里的Type应该是Datacontext对象的Type属性值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    相关资源
    最近更新 更多