Search code examples
c#wpfmvvmradio-button

How to pass content property of Control as CommandParameter WPF


I have WPF project that has 19 radio button. I want to set Command property each of them. To separate all command actions, I want to send RadioButton Content property as a CommandParameter.

I try to define setter for CommandParameter property and I set {Binding Path=Content}for Value as you can see below.

But it is not work.

 <Style TargetType="RadioButton">
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="Margin" Value="5"/>
        <Setter Property="Command" Value="{Binding SelectionChangedCommand}"/>
        <Setter Property="CommandParameter" Value="{Binding Path=Content}"/>
    </Style>
          

 <RadioButton Content="Area"/>
            <RadioButton Content="Unit"
                         Grid.Row="1"/>
            
            <RadioButton Content="Transom Count"
                         Grid.Row="2"
                         CommandParameter="Transom Count"/>
            <RadioButton Content="Mullion Count"
                         Grid.Row="3"/>
            <RadioButton Content="Transom And Mulliom Count"
                         Grid.Row="4"/>
            <RadioButton Content="Frame Count"
                         Grid.Row="5"/>
            <RadioButton Content="Frame + Transom + Mullion Count"
                         Grid.Row="6"/>
            <RadioButton Content="Corner Points"
                         Grid.Row="7"/>
            <RadioButton Content="T Connections"
                         Grid.Row="8"/>

            <RadioButton Content="Left"
                         Grid.Column="1"/>
            <RadioButton Content="Right"
                         Grid.Column="1"
                         Grid.Row="1"/>
            <RadioButton Content="Top"
                         Grid.Column="1"
                         Grid.Row="2"/>
            <RadioButton Content="Bottom"
                         Grid.Column="1"
                         Grid.Row="3"/>
            <RadioButton Content="Perimeter"
                         Grid.Column="1"
                         Grid.Row="4"/>
            <RadioButton Content="Transom Length"
                         Grid.Column="1"
                         Grid.Row="5"/>
            <RadioButton Content="Mullion Length"
                         Grid.Column="1"
                         Grid.Row="6"/>
            <RadioButton Content="Transom + Mullion Length"
                         Grid.Column="1"
                         Grid.Row="7"/>
            <RadioButton Content="Perimeter + Transom + Mullion Length"
                         Grid.Column="1"
                         Grid.Row="8"/>

enter image description here


Solution

  •  <Style TargetType="RadioButton">
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="Margin" Value="5"/>
            <Setter Property="Command" Value="{Binding SelectionChangedCommand}"/>
            <Setter Property="CommandParameter" Value="{Binding Content, RelativeSource={RelativeSource Self}}"/>
        </Style>