如何设置 RadioButton.IsChecked,如果是 Content 获得焦点

How to set RadioButton.IsChecked, if it's Content got focus

在我的 WPF 应用程序中,我有一组 RadioButton,其中一个元素定义如下:

<RadioButton Grid.Row="2" Grid.Column="1"
                Margin="5"
                HorizontalContentAlignment="Stretch"
                GroupName="GroupDatetimeTo">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Column="0"
                    Margin="5,5,15,5"
                    VerticalAlignment="Center"
                    Text="Exactly">
        </TextBlock>

        <mah:DateTimePicker Grid.Column="1"
                            Margin="5"
                            VerticalAlignment="Center" HorizontalAlignment="Stretch"
                            SelectedDateTime="{Binding Path=DatetimeTo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
        </mah:DateTimePicker>
    </Grid>
</RadioButton>

我希望当 DateTimePicker 控件获得焦点时 - RadioButton 会自动被选中。但是,这不会发生。我该如何解决?

绑定选中状态

您可以将绑定添加到 IsChecked 属性 以监视 DateTimePicker 的键盘焦点。

<RadioButton IsChecked="{Binding ElementName=MyDateTimePicker, Mode=OneWay, Path=IsKeyboardFocusWithin}" ... >

这里,MyDateTimePicker是你RadioButton中的DateTimePicker的名字。

<mahapps:DateTimePicker x:Name="MyDateTimePicker" ... />

此方案不适用,如需绑定IsChecked否则

检查重点附件属性

另一种解决方案是创建一个附加的 属性,当它附加到的控件获得键盘焦点时,可以启用它来监督。然后,它向上搜索可视化树以找到父级 RadioButton 并将 IsChecked 设置为 true。这样,您仍然可以绑定 属性.

public static class RadioButtonProperties
{
   public static readonly DependencyProperty CheckOnFocusProperty = DependencyProperty.RegisterAttached(
      "CheckOnFocus", typeof(bool), typeof(RadioButtonProperties), new PropertyMetadata(false, OnCheckOnFocusChanged));

   private static void OnCheckOnFocusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   {
      if (!(d is FrameworkElement frameworkElement))
         return;

      if ((bool)e.NewValue)
         frameworkElement.GotKeyboardFocus += OnGotKeyboardFocus;
      else
         frameworkElement.GotKeyboardFocus -= OnGotKeyboardFocus;
   }

   public static bool GetCheckOnFocus(DependencyObject dependencyObject)
   {
      return (bool)dependencyObject.GetValue(CheckOnFocusProperty);
   }

   public static void SetCheckOnFocus(DependencyObject dependencyObject, bool value)
   {
      dependencyObject.SetValue(CheckOnFocusProperty, value);
   }

   private static void OnGotKeyboardFocus(object sender, RoutedEventArgs e)
   {
      var radioButton = FindParent<RadioButton>((DependencyObject)sender);
      if (radioButton != null)
         radioButton.IsChecked = true;
   }

   private static T FindParent<T>(DependencyObject child) where T : DependencyObject
   {
      var parentObject = VisualTreeHelper.GetParent(child);

      if (parentObject == null)
         return null;

      if (parentObject is T parent)
         return parent;

      return FindParent<T>(parentObject);
   }
}

要启用它,请在目标 DateTimePicker.

上将附加的 CheckOnFocus 属性 设置为 True
<mahapps:DateTimePicker local:RadioButtonProperties.CheckOnFocus="True" ...>