一般替换MultiBinding节点中的Converter



我有一些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>

我想为每个RadioButton选项更改Converter,但其他一切都保持不变(所有绑定)。我不想为此复制所有的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创建一个"Visibility"属性,但这会向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冗余?

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

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

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


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

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

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

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

相关内容

  • 没有找到相关文章

最新更新