wpf 中的 HasEffectiveKeyboardFocus 和 IsKeyboardFocused 有什么区别

What is the difference between HasEffectiveKeyboardFocus & IsKeyboardFocused in wpf

提问人:JSR 提问时间:7/17/2023 更新时间:7/17/2023 访问量:24

问:

我们有一个可编辑的组合框(Cb1),它有一个自定义的弹出窗口和另一个可编辑的组合框(Cb2)。下面是示例代码

 public class ExComboBox : ComboBox
    {            
      public static readonly DependencyProperty CustomPopupProperty = DependencyProperty.Register(
        "CustomPopup", 
        typeof(FrameworkElement), 
        typeof(ExComboBox),
        new PropertyMetadata((d, e) =>
        {

        }));       
   
       public FrameworkElement CustomPopup
       {
          get { return (FrameworkElement)GetValue(CustomPopupProperty); }
          set { SetValue(CustomPopupProperty, value); }
       }

      static ExComboBox()
      {
          DefaultStyleKeyProperty.OverrideMetadata(typeof(ExComboBox), new FrameworkPropertyMetadata(typeof(ExComboBox)));
      }
}

在 XAML 中,我们有一个 CustomControl,它将显示绑定的“Popup”。

<ControlTemplate x:Key="ExComboBoxTemplate" TargetType="{x:Type local:ExComboBox}">
<Grid x:Name="templateRoot" SnapsToDevicePixels="true" >
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0" />
  </Grid.ColumnDefinitions>
  <Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom" >
    <Themes:SystemDropShadowChrome x:Name="shadow" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=templateRoot}" >
      <Border x:Name="dropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
        <ContentControl Content="{TemplateBinding CustomPopup}"  />
      </Border>
    </Themes:SystemDropShadowChrome>
  </Popup>
  <ToggleButton x:Name="toggleButton" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxToggleButton}"  />
  <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Content="{TemplateBinding SelectionBoxItem}" />
</Grid>
  <ExComboBox x:Name="Cb1" IsEditable="True" Text="{Binding Value1, UpdateSourceTrigger=LostFocus}" 
                                 HorizontalAlignment="Left">
                        <ExComboBox.CustomPopup >                                   
                            <StackPanel Margin="5">
                                <GroupBox>
                                    <GroupBox.Header>
                                        <TextBlock Text="My Header" />
                                    </GroupBox.Header>
                                    <StackPanel Margin="5">
                                        <TextBlock Text="Select or Enter value" />
                                        
                                        <ComboBox x:Name="Cb2" 
                                                  IsEditable="True" 
                                                  Text="{Binding Value2}"                               
                                                  IsTextSearchEnabled="True" 
                                                  IsSynchronizedWithCurrentItem="True" 
                                                  ItemsSource="{Binding Value2Collection}" 
                                                  VerticalAlignment="Center"  />               
                                           <!-- Other controls -->

                                    </StackPanel>
                                </GroupBox>

                            </StackPanel>
                        </ExComboBox.CustomPopup >
                    </ExComboBox>

我们还对在两个组合框中输入的值进行了一些验证。 当我尝试在 Cb2 中输入值时,首先调用 Cb1 的 previewTextInput,而不是 Cb2。我预计 Cb2 将首先处理,因为 Cb2 获得了键盘焦点。查看属性时,“HasEffectiveKeyboardFocus”始终显示正确的值,但不会显示“IsKeyboardFocused”。

你能告诉我HasEffectiveKeyboardFocus和IskeyboardFocused之间的区别吗?或者我怎样才能获得正确的焦点元素,这样我就可以在 Cb1 中输入值时忽略 Cb2 的 previewTextInput。 (我不能使用'HasEffectiveKeyboardFocus,因为它是受保护的) 提前致谢。

WPF 组合框 弹出窗口 自定义控件 键盘焦点

评论


答: 暂无答案