【问题标题】:setting ConverterParameter in style在样式中设置 ConverterParameter
【发布时间】:2013-06-06 08:59:41
【问题描述】:
<Window.Resources>
    <ResourceDictionary>            
        <Style TargetType="{x:Type propgrid:PropertyGridDataAccessorItem}">
            <Style.Triggers>
               <Trigger Property="DataAccessorType" Value="Category">
                   <Setter Property="IsExpanded" Value="{Binding DisplayName, RelativeSource={x:Static RelativeSource.Self}, ConverterParameter=???, Converter={local:ExpandedCategoryConverter}}"/>
               </Trigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>
</Window.Resources>

问题是我不知道如何将我的 ViewModel 中的属性作为 ConverterParameter 发送。我想要像 ConverterParameter="{Binding MyValue}" 这样的东西,但这是不可能的。我尝试了这样的多重绑定:

<Trigger Property="DataAccessorType" Value="Category">
    <Setter Property="IsExpanded">   
        <Setter.Value>
            <MultiBinding Converter="{local:ExpandedCategoryConverter}">
                <Binding Path="DisplayName"/>
                <Binding Path="MyProperty"/>
            </MultiBinding>
        </Setter.Value>
    </Setter>
</Trigger>

但我的属性始终为空。

有人知道如何处理这个问题吗?

提前谢谢

【问题讨论】:

  • VS 输出窗口中是否有任何绑定错误消息?至少在单个绑定中你设置了RelativeSource,但你没有在 MultiBinding 中这样做。
  • 在调试模式下的转换器类中,DisplayName 很好,但 ExpandCategory 是 - 未设置({DependencyProperty.UnsetValue})。有什么想法可以解决吗?
  • 什么是 ExpandCategory?在你的问题中找不到。是否有绑定错误消息?运行应用程序时,请查看 Visual Studio 中的输出窗口。
  • 这是我在 viewModel 中的属性的名称。这里的代码与 MyProperty 相同
  • 绑定错误信息怎么办?

标签: wpf xaml mvvm binding


【解决方案1】:

你得到DependencyProperty.UnsetValue for MyProperty(或ExpandCategory),因为绑定不会产生值。 IMultiValueConverter.Convert 方法的 values 参数的 MSDN 文档说:

价值观

MultiBinding 中源绑定的值数组 产生。 UnsetValue 值表示源绑定有 没有为转化提供价值。

您必须确保&lt;Binding Path="MyProperty"/&gt; 有效。我猜你没有正确设置绑定的来源。

【讨论】:

  • 谢谢你的帮助,我找到了这样一行:RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Window}"
猜你喜欢
  • 2012-02-03
  • 2012-03-12
  • 2017-01-28
  • 1970-01-01
  • 2012-12-10
  • 1970-01-01
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多