2

我有一种情况,我需要使用TabKey从一个控件到另一个控件的选项卡。条件是焦点永远不应该转到没有用户输入的控件上Text,但不知何故ComboBoxList DatePicker3个控件之后的焦点Focus转到虚线Rectangle(可能是一个Grid,StackPanel,我无法找出)在控制组进入控制之前。我已经在 Google 中进行了非常彻底的搜索,并通过堆栈溢出来寻找解决方案,但似乎没有一个有效。

我尝试过的各种解决方案:

1)在这里,我FocusVisualStyle在启动时将属性设置为 nullGridStackPanels。创建了一个新类:

<StackPanel views:FocusVisualTreeChanger.IsChanged="True" Name="parentStackPanel" Orientation="Vertical" Style="{DynamicResource key1}" FocusVisualStyle="{x:Null}">

public class FocusVisualTreeChanger
{
    public static bool GetIsChanged(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsChangedProperty);
    }

    public static void SetIsChanged(DependencyObject obj, bool value)
    {
        obj.SetValue(IsChangedProperty, value);
    }

    public static readonly DependencyProperty IsChangedProperty =
        DependencyProperty.RegisterAttached("IsChanged", typeof(bool), typeof(FocusVisualTreeChanger), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits, IsChangedCallback));

    private static void IsChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (true.Equals(e.NewValue))
        {
            FrameworkContentElement contentElement = d as FrameworkContentElement;
            if (contentElement != null)
            {
                contentElement.FocusVisualStyle = null;
                return;
            }

            FrameworkElement element = d as FrameworkElement;
            if (element != null)
            {
                element.FocusVisualStyle = null;
            }
        }
    }
}

不工作。

我尝试将FocusVisualStyle属性设置为null,如果我放置一个断点但焦点矩形不会消失Grid,似乎会通过代码隐藏:StackPanel

<Grid Name="AppointmentGrid" Style="{DynamicResource key2}" FocusVisualStyle="{x:Null}">

Style style = new Style { TargetType = typeof(Grid) };
style.Setters.Add(new Setter(Grid.FocusVisualStyleProperty, null));
style.Setters.Add(new Setter(Grid.FocusableProperty, false));
Application.Current.Resources["key2"] = style;
Application.Current.Resources["key3"] = style;
Application.Current.Resources["key4"] = style;
Style style1 = new Style { TargetType = typeof(StackPanel) };
style1.Setters.Add(new Setter(StackPanel.FocusVisualStyleProperty, null));
style1.Setters.Add(new Setter(StackPanel.FocusableProperty, false));
Application.Current.Resources["key1"] = style1; 

任何人都可以帮助我解决我尚未尝试过的解决方案。stackoverflow 解决方案中的任何一个似乎都不起作用。我还设置了 Focusable=false 以防万一,但这似乎也无济于事。

我还读到:

删除用户控件上的焦点矩形

WPF 控件移除焦点和悬停效果(FocusVisualStyle?!)

WPF:删除样式列表框中焦点项目周围的虚线边框

这就是我认为我被困在的地方。我在其中一个搜索网站上找到的评论。

这是更改 DP 的默认值的好方法,但在控件样式显式更改属性值的情况下,它无济于事。不幸的是,FocusVisualStyle这样的财产之一。更具体地说,像ButtonComboBoxListBox等控件的样式倾向于显式包含 FocusVisualStyle 属性的 Setter。此设置器将覆盖您通过覆盖元数据建立的默认值。

有人可以提出一个适用于我的解决方案。我有一个用户控件

<UserControl 
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:csla="http://schemas.lhotka.net/4.2.0/xaml" 
          xmlns:input="clr-             
         FocusVisualStyle="{x:Null}"
         Focusable="False"
         xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
         mc:Ignorable="d" d:DesignWidth="948"
         IsTabStop="False" 
         TabIndex="-1">

<StackPanel views:FocusVisualTreeChanger.IsChanged="True" Name="parentStackPanel" Orientation="Vertical" Style="{DynamicResource key1}" FocusVisualStyle="{x:Null}"><Grid Name="AppointmentGrid" Style="{DynamicResource key2}" FocusVisualStyle="{x:Null}">

谢谢迪伦

4

0 回答 0