1

I have some RadioButton’s that I want to change the Visibility of based on multiple properties in the ViewModel. Each RadioButton’s Visibility will vary based on the same list of properties. I have the following 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>

I would like to change the Converter for each RadioButton option, but everything else would remain the same (all the Bindings). I don’t want to duplicate all the xaml code for this. I originally tried creating a style for the RadioButton, but I could not figure out how to pass the Converter to the Style resource:

<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>

I could create a “Visibility” property for each RadioButton in the ViewModel, but that adds a lot of code to the ViewModel and addition PropertyChanged event handling. Is there an easy way to generically bubble up the MultiBinding so I don’t duplicate the code throughout the entire xaml file? Is there a different approach I should be taking?

Update - Adding a more complex example

<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>

How do I reduce the MultiBinding redundancy here?

SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
  • Have you tried Value Converter where you could write your logic and return the Visibility – D_Learning Jul 31 '15 at 15:31
  • @D_Learning - Thats what `Rb1Visibility` does. It returns the `Visibility` based on `Value1` and `Value2`. The implementation would be different for each `RadioButton`. – SwDevMan81 Jul 31 '15 at 15:37
  • You can try with MultiValueConerter, see this question that might help you... http://stackoverflow.com/questions/16926709/passing-values-to-ivalueconverter?answertab=active#tab-top. you can pass the whole object and pass the parameter as Value1 and/or Value 2. For passing parameters see this answer: http://stackoverflow.com/questions/3978937/how-to-pass-an-integer-as-converterparameter – D_Learning Jul 31 '15 at 15:46
  • @D_Learning - I'm not having any problems with the Converter or parameters to it. – SwDevMan81 Jul 31 '15 at 15:50

1 Answers1

1

would like to change the Converter for each RadioButton option,

Create a new MultiValueConverter which takes an extra parameter (the radio button option) and then simply route the call to the appropriate converter based on that option.

One can put the option in the Tag property on the control's Xaml.


Example

Its not clear to me what that option you mention is, so in my example let us uniquely identify each Radio Button by its Tag property and use the routing converter to find the appropriate converter based off of the Tag supplied.

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

Then change the style to use the new converter with the new parameter:

<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>
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • Sorry, I only gave the one example to keep the question short. Imagine the exact same RadioButton node with a different say `Rb2Visibility` assigned to the Converter – SwDevMan81 Jul 31 '15 at 17:12
  • @SwDevMan81 At what point would the visibility converter be assigned/re-assigned? Is this to be done in code or a binding? – ΩmegaMan Jul 31 '15 at 18:25
  • Preferably in the binding – SwDevMan81 Jul 31 '15 at 18:27
  • @SwDevMan81, No, I know its in the binding, I mean what triggers the change to change a binding? – ΩmegaMan Jul 31 '15 at 18:50
  • The binding itself would never change. A change in `Value1` or `Value2` would "trigger" the converter to run and redetermine the Visibility property and update UI accordingly. – SwDevMan81 Aug 03 '15 at 12:31
  • @SwDevMan81 if the binding to a converter never changes, then the suggestion I present about a centralized converter which switches based off a specific controls solves the problem but does not appear to answer you question. – ΩmegaMan Aug 03 '15 at 13:18
  • I would agree,. Its more of a hack using the Tag property to identify which Button is being clicked and then adding additional logic in a single converter. It could be useful for someone, but it doesn't solve my problem, thanks for the answer – SwDevMan81 Aug 03 '15 at 13:58