GreyCloud
GreyCloud

Reputation: 3100

WPF Bind to selected Radio Button

I have a Radiobutton group:

 <RadioButton Name="_CreateGraph" GroupName="MapOrGraph" Content="Create Graph" />
 <RadioButton Name="_CreateMap"   GroupName="MapOrGraph" Content="Create Map"   />

How do I bind the content of a label to the selected radio button's content?

e.g. the label should say Create Graph or Create Map depending on which radio box is created?

Upvotes: 3

Views: 2457

Answers (2)

phil
phil

Reputation: 287

If you have only two RadioButtons, this is a quick & dirty solution:

    <Label>
        <Label.Style>
            <Style TargetType="{x:Type Label}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsChecked, ElementName=_CreateGraph}" Value="True">
                        <Setter Property="Content" Value="{Binding Content, ElementName=_CreateGraph}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding IsChecked, ElementName=_CreateMap}" Value="True">
                        <Setter Property="Content" Value="{Binding Content, ElementName=_CreateMap}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Label.Style>
    </Label>

Upvotes: 1

dovid
dovid

Reputation: 6452

<Label Content="{Binding SelectedValue.Content, ElementName=list}"  />

<ListBox x:Name="list" SelectedIndex="1"  >
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem" >
                        <RadioButton GroupName="a" Content="{TemplateBinding Content}" IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}  }"  />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBoxItem>Create Graph</ListBoxItem>
    <ListBoxItem>Create Map</ListBoxItem>
</ListBox>

I think you can make it shorter than what I did.

Upvotes: 4

Related Questions