通常在MultiBinding节点中替换Converter

时间:2015-07-31 15:23:02

标签: c# wpf xaml data-binding multibinding

我有一些RadioButton’s我希望根据ViewModel中的多个属性更改Visibility。每个RadioButton’s可见性将根据相同的属性列表而有所不同。我有以下xaml:

<RadioButton Command="{Binding Path=SomeCommand}"
             CommandParameter="SomeCommandParameter"
             Content="RB 1">
   <RadioButton.Visibility>
      <MultiBinding Converter="{StaticResource Rb1Visibility}">
         <Binding Path="Value1"
                  RelativeSource="{RelativeSource Self}" />
         <Binding Path="Value2"
                  RelativeSource="{RelativeSource Self}" />
      </MultiBinding>
   </RadioButton.Visibility>
</RadioButton>

我想为每个Converter选项更改RadioButton,但其他所有内容都保持不变(所有绑定)。我不想为此复制所有xaml代码。我最初尝试为RadioButton创建一个样式,但我无法弄清楚如何将Converter传递给Style资源:

<Style x:Key="RbVisibilityStyle"
       TargetType="{x:Type RadioButton}">
   <Setter Property="Visibility">
      <Setter.Value>
         <MultiBinding Converter="{???? Pass in converter ?????}">
            <Binding Path="Value1"
                     RelativeSource="{RelativeSource Self}" />
            <Binding Path="Value2"
                     RelativeSource="{RelativeSource Self}" />
         </MultiBinding>
      </Setter.Value>
   </Setter>
</Style>

我可以为ViewModel中的每个RadioButton创建一个“可见性”属性,但这会为ViewModel添加大量代码并添加PropertyChanged事件处理。 是否有一种简单的方法来泛滥MultiBinding所以我不会在整个xaml文件中复制代码?我应该采取不同的方法吗?

更新 - 添加更复杂的示例

<RadioButton Command="{Binding Path=SomeCommand}"
             CommandParameter="SomeCommandParameter"
             Content="RB 1">
   <RadioButton.Visibility>
      <MultiBinding Converter="{StaticResource Rb1Visibility}">
         <Binding Path="Value1"
                  RelativeSource="{RelativeSource Self}" />
         <Binding Path="Value2"
                  RelativeSource="{RelativeSource Self}" />
      </MultiBinding>
   </RadioButton.Visibility>
</RadioButton>
<RadioButton Command="{Binding Path=SomeCommand2}"
             CommandParameter="SomeCommandParameter2"
             Content="RB 2">
   <RadioButton.Visibility>
      <MultiBinding Converter="{StaticResource Rb2Visibility}">
         <Binding Path="Value1"
                  RelativeSource="{RelativeSource Self}" />
         <Binding Path="Value2"
                  RelativeSource="{RelativeSource Self}" />
      </MultiBinding>
   </RadioButton.Visibility>
</RadioButton>
<RadioButton Command="{Binding Path=SomeCommand3}"
             CommandParameter="SomeCommandParameter3"
             Content="RB 3">
   <RadioButton.Visibility>
      <MultiBinding Converter="{StaticResource Rb3Visibility}">
         <Binding Path="Value1"
                  RelativeSource="{RelativeSource Self}" />
         <Binding Path="Value2"
                  RelativeSource="{RelativeSource Self}" />
      </MultiBinding>
   </RadioButton.Visibility>
</RadioButton>

如何在此处减少MultiBinding冗余?

1 个答案:

答案 0 :(得分:1)

  

想要为每个RadioButton选项更改Converter,

创建一个新的MultiValueConverter,它接受一个额外的参数(单选按钮选项),然后根据该选项简单地将调用路由到相应的转换器。

可以将选项放在控件的Xaml上的Tag属性中。

实施例

我不清楚你提到的那个选项是什么,所以在我的例子中,让我们通过Tag属性唯一地标识每个单选按钮,并使用路由转换器根据{{找到适当的转换器1}}提供。

Tag

然后更改样式以使用带有新参数的新转换器:

<RadioButton Tag="1"/><RadioButton Tag="2"/>