WPF数据绑定和级联转换器?

Joa*_*mer 11 data-binding wpf xaml binding converter

我想知道在使用wpf数据绑定时是否可以级联转换器.例如

<SomeControl Visibility="{Binding Path=SomeProperty, Converter={StaticResource firstConverter}, Converter={StaticResource secondConverter}}"/>
Run Code Online (Sandbox Code Playgroud)

它是否可能或者我是否必须创建一个结合了转换器A和B的功能的定制转换器?

Bra*_*ach 18

您可能正在寻找类似于Josh Smith的" 管道值转换器 " 的解决方案.

在他的文章中,他提出以下内容:

<local:ValueConverterGroup x:Key="statusDisplayNameGroup">
  <local:IntegerStringToProcessingStateConverter  />
  <local:EnumToDisplayNameConverter />
</local:ValueConverterGroup> 
Run Code Online (Sandbox Code Playgroud)

然后使用多值转换器如下:

<TextBlock Text="{Binding XPath=@Status, 
             Converter={StaticResource statusDisplayNameGroup}}" />
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!


Enr*_*lio 7

您可以尝试使用MultiBinding,并将两次绑定到同一个源,但在单个绑定上使用不同的转换.就像是:

<SomeControl>
    <SomeControl.Visibility>
        <MultiBinding Converter="{StaticResource combiningConverter}">
            <Binding Path="SomeProperty" Converter="{StaticResource firstConverter}"/>
            <Binding Path="SomeProperty" Converter="{StaticResource secondConverter}"/>
        </MultiBinding>
    </SomeControl.Visibility>
</SomeControl>
Run Code Online (Sandbox Code Playgroud)

然后在' combineConverter '中放置逻辑以组合来自两个绑定的值.