我有一种情况,我需要使用TabKey
从一个控件到另一个控件的选项卡。条件是焦点永远不应该转到没有用户输入的控件上Text
,但不知何故ComboBox
,List DatePicker
3个控件之后的焦点Focus
转到虚线Rectangle
(可能是一个Grid
,StackPanel,我无法找出)在控制组进入控制之前。我已经在 Google 中进行了非常彻底的搜索,并通过堆栈溢出来寻找解决方案,但似乎没有一个有效。
我尝试过的各种解决方案:
1)在这里,我FocusVisualStyle
在启动时将属性设置为 nullGrid
和StackPanels
。创建了一个新类:
<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?!)
这就是我认为我被困在的地方。我在其中一个搜索网站上找到的评论。
这是更改 DP 的默认值的好方法,但在控件样式显式更改属性值的情况下,它无济于事。不幸的是,FocusVisualStyle
这样的财产之一。更具体地说,像Button
、ComboBox
、ListBox
等控件的样式倾向于显式包含 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}">
谢谢迪伦