【问题标题】:WPF Binding to a RadioButton GroupNameWPF 绑定到 RadioButton GroupName
【发布时间】:2017-12-23 22:19:37
【问题描述】:

我有一个使用DataTemplate 的动态ListView,如下所示:

<ListView.ItemTemplate>
<DataTemplate>
    <Border BorderThickness="0,0,0,1" BorderBrush="#ccc">
        <RadioButton GroupName="MyRadioButtonGroup">
            <StackPanel Orientation="Horizontal" Margin="0,0,0,5" Width="{Binding ActualWidth, ElementName=CheckoutGrid}">
                <TextBlock Text="{Binding Path=Test1, StringFormat='{}{0} - '}" />
                <TextBlock Text="{Binding Path=Test2, StringFormat='{} test {0} - '}" />
                <TextBlock Text="{Binding Path=Test, StringFormat='{} test {0}'}" />
            </StackPanel>
        </RadioButton>
    </Border>
</DataTemplate>
</ListView.ItemTemplate>

我想禁用一个按钮,直到其中一个 RadioButton IsChecked:

<RadioButton Padding="15,10" VerticalAlignment="Center">
<RadioButton.Style>
    <Style TargetType="RadioButton" BasedOn="{StaticResource styleToggleButton4}">
        <Setter Property="Visibility" Value="Collapsed" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=MyRadioButtonGroup, Path=IsChecked}" Value="False">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
            </Style.Triggers>
    </Style>
</RadioButton.Style>
<TextBlock FontWeight="SemiBold" FontSize="14" Margin="0">NEXT</TextBlock>
</RadioButton>

所以问题是我不知道如何正确绑定到RadioButton GroupName="MyRadioButtonGroup"。您将在上面的DataTrigger 中看到我正在尝试Binding="{Binding ElementName=MyRadioButtonGroup, Path=IsChecked}" Value="False",但这对我不起作用,因为显然它是GroupName 而不是x:Name

关于如何正确处理此问题的任何建议?如果可能的话,我宁愿在前端处理这个问题。

【问题讨论】:

    标签: wpf xaml binding radio-button


    【解决方案1】:

    您可以将单选按钮绑定到命令,然后使用 isChecked 作为参数。然后绑定到基于此的布尔值。不过,我认为这可能更像是一种解决方法。

    <RadioButton
            Command="{Binding MyCommand}"
            CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}"
            GroupName="radio" />
    <RadioButton
            Command="{Binding MyCommand}"
            CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}"
            GroupName="radio" />
    

    然后在代码隐藏或 VM 中,需要实现 ICommand 接口和 INotifyPropertyChanged

        public bool IsChecked
        {
            get { return isChecked;}
            set
            {
                isChecked = value;
                OnPropertyChanged();
            }
        }
    
        private void OnMyCommand(object obj)
        {
            if(obj is bool)
            {
                IsChecked = (bool)obj;
            }
        }
    

    【讨论】:

      【解决方案2】:

      组没有可以绑定到的 IsChecked 属性。

      如果您没有源属性来跟踪是否至少有一个选中的RadioButton 并且“想要在前端处理这个” - 我猜这意味着在视图中 - 你可以写一些代码只是将ListViewTag 属性设置为true/false,具体取决于是否选择了RadioButton。这是一个单行:

      private void ListView_Checked(object sender, RoutedEventArgs e) => lv.Tag = true;
      

      <ListView x:Name="lv" ToggleButton.Checked="ListView_Checked">
          <ListView.ItemTemplate>
              ...
          <ListView.ItemTemplate>
      </ListView>
      
      <RadioButton Padding="15,10" VerticalAlignment="Center">
          <RadioButton.Style>
              <Style TargetType="RadioButton" BasedOn="{StaticResource styleToggleButton4}">
                  <Setter Property="Visibility" Value="Collapsed" />
                  <Style.Triggers>
                      <DataTrigger Binding="{Binding ElementName=lv, Path=Tag}" Value="True">
                          <Setter Property="Visibility" Value="Visible" />
                      </DataTrigger>
                  </Style.Triggers>
              </Style>
          </RadioButton.Style>
          <TextBlock FontWeight="SemiBold" FontSize="14" Margin="0">NEXT</TextBlock>
      </RadioButton>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-04
        • 2021-04-02
        • 2011-03-22
        • 1970-01-01
        • 2017-04-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多