C# WPF绑定到选定单选按钮
我有一个单选按钮组:C# WPF绑定到选定单选按钮,c#,wpf,C#,Wpf,我有一个单选按钮组: <RadioButton Name="_CreateGraph" GroupName="MapOrGraph" Content="Create Graph" /> <RadioButton Name="_CreateMap" GroupName="MapOrGraph" Content="Create Map" /> 如何将标签的内容绑定到选定单选按钮的内容 e、 g.标签上应注明“创建图形”或“创建地图”,具体取决于创建的单选框?
<RadioButton Name="_CreateGraph" GroupName="MapOrGraph" Content="Create Graph" />
<RadioButton Name="_CreateMap" GroupName="MapOrGraph" Content="Create Map" />
如何将标签的内容绑定到选定单选按钮的内容
e、 g.标签上应注明“创建图形”或“创建地图”,具体取决于创建的单选框?如果只有两个单选按钮,则这是一个快速且肮脏的解决方案:
<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放在列表框中(并将IsChecked绑定到Item.IsSelected),将label绑定到selecteditem\value。@lomed,显示代码,我接受你的答案。但是,我不想改变收音机盒的外观和感觉,因为某些原因,我无法取消收音机盒的设置,我也可以选择两者?我还认为这个解决方案需要一个值转换器,因为我看不到标签的变化值?您可以选择两个单选按钮,因为它们不在组中。将
GroupName=“mapograph”
添加到上面代码中的RadioButton,它应该可以工作。我已经更新了答案。只需在XAML中复制标签并检查名称(_CreateGraph,_CreateMap)。这应该行得通。
<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>