绑定RadioButton GroupName

时间:2020-04-08 03:33:14

标签: c# wpf

我有一个使用“ DataTemplate”的“ ListView”,如下所示(“ UserControl”):

<ListBox VerticalAlignment="Stretch"
       SelectionMode="Single" ItemsSource="{Binding Source}" Visibility="{Binding Visibility}">
<ListBox.ItemsPanel>
  <ItemsPanelTemplate>
    <VirtualizingStackPanel IsItemsHost="True" Orientation="{Binding Orientation}"/>
  </ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.Resources>
  <Style TargetType="ListBoxItem">
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="ListBoxItem">
          <RadioButton
            GroupName="{Binding GroupName}"
            Foreground="Black"
            Margin="5,2"
            IsChecked="{TemplateBinding IsSelected}">
            <RadioButton.Resources>
              <!--Unchecked state-->
              <SolidColorBrush x:Key="MaterialDesignCheckBoxOff" Color="#ced4da"/>
              <!--Checked state-->
              <SolidColorBrush x:Key="PrimaryHueMidBrush" Color="#3e97eb"/>
            </RadioButton.Resources>
            <ContentPresenter/>
          </RadioButton>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ListBox.Resources>

我在“ MainWindowView”中使用了四个“ ListBox”(“ UserControl”),因此必须使用“ GroupName”。但是“ Binding GroupName”不起作用。

更新 我的问题解决了:

IsChecked="{Binding Path=IsSelected,
                            RelativeSource={RelativeSource TemplatedParent},
                            Mode=TwoWay}"

0 个答案:

没有答案
相关问题