我有一個單選按鈕組:WPF綁定到選定的單選按鈕
<RadioButton Name="_CreateGraph" GroupName="MapOrGraph" Content="Create Graph" />
<RadioButton Name="_CreateMap" GroupName="MapOrGraph" Content="Create Map" />
如何綁定標籤到選定的單選按鈕的內容內容?
例如標籤應該說創建圖表或創建地圖取決於哪個單選框被創建?
我有一個單選按鈕組:WPF綁定到選定的單選按鈕
<RadioButton Name="_CreateGraph" GroupName="MapOrGraph" Content="Create Graph" />
<RadioButton Name="_CreateMap" GroupName="MapOrGraph" Content="Create Map" />
如何綁定標籤到選定的單選按鈕的內容內容?
例如標籤應該說創建圖表或創建地圖取決於哪個單選框被創建?
<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>
我想你可以把它比我做短。
如果你只有兩個單選按鈕,這是一個快速&骯髒的解決方案:
<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>
您可以將RB放入listBox(並將IsChecked綁定到Item.IsSelected)並將標籤綁定到selecteditem \ value。 – dovid
@lomed,顯示我的代碼,我接受你的答案。但我不想改變收音機的外觀 – GreyCloud