c# - 在当前具有焦点的任何元素周围放置一个鲜红色框的可重用方法?

标签 c# wpf

我有一些主要包含组合框、文本框和复选框的窗口。当您单击一个以获得焦点时,我需要一种方法来用彩色框(老板的命令)勾勒出它们的轮廓。有没有比覆盖所有这些控件的默认样式更容易做到这一点的方法?我以前从未这样做过,所以我需要花很多时间才能弄明白。

最佳答案

您可以尝试添加 FocusVisualStyle到需要不同焦点矩形样式的控件。

来自上面的链接

The second mechanism is to provide a separate style as the value of the FocusVisualStyle property; the "focus visual style" creates a separate visual tree for an adorner that draws on top of the control, rather than changing the visual tree of the control or other UI element by replacing it.

在你的 Window 的 Xaml 中有这样的东西

<Window.Resources>
    <Style x:Key="NewFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border>
                        <Rectangle Stroke="Red"  Margin="2" StrokeThickness="1"  StrokeDashArray="1 2" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

或您的 Application.Xaml 文件。

<Application.Resources>
    <Style x:Key="NewFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border>
                        <Rectangle Stroke="Red"  Margin="2" StrokeThickness="1"  StrokeDashArray="1 2" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

用法:

<ComboBox FocusVisualStyle="{StaticResource NewFocusVisual}"  Height="23" HorizontalAlignment="Left" Margin="238,102,0,0" Name="ComboBox1" VerticalAlignment="Top" Width="120" />
<CheckBox FocusVisualStyle="{StaticResource NewFocusVisual}" Content="CheckBox" Height="16" HorizontalAlignment="Left" Margin="238,71,0,0" Name="CheckBox2" VerticalAlignment="Top" />
<TextBox FocusVisualStyle="{StaticResource NewFocusVisual}" Height="23" HorizontalAlignment="Left" Margin="238,144,0,0" Name="TextBox1" VerticalAlignment="Top" Width="120" />

如果您希望针对每种类型的焦点事件更改焦点矩形,Microsoft 声明:

From Microsoft: Focus visual styles act exclusively for keyboard focus. As such, focus visual styles are a type of accessibility feature. If you want UI changes for any type of focus, whether via mouse, keyboard, or programmatically, then you should not use focus visual styles, and should instead use setters and triggers in styles or templates that are working from the value of general focus properties such as IsFocused or IsFocusWithin.


试一试它适用于 TextBox 还没有检查你的其他控件

<Application.Resources>
    <Style TargetType="TextBox" >
        <Style.Triggers>
            <Trigger Property="IsFocused" Value="True">
                <Setter Property="Control.BorderBrush" Value="Red"  />
                <Setter Property="Control.BorderThickness" Value="3" />
            </Trigger>
        </Style.Triggers>
    </Style>

</Application.Resources>

关于c# - 在当前具有焦点的任何元素周围放置一个鲜红色框的可重用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10771504/

相关文章:

c# - 如何用文本替换 gridview 中的复选框?

c# - C#中issue/response串口处理

c# - 关于 DLR (.NET 4.0 beta 1) 的最佳信息来源是什么?

wpf - 如何将序列号添加到行标题

c# - 如何根据 wpf 中的值使特定颜色变暗或变亮?

c# - JsonConvert.SerializeObject 将自定义类型属性传递给父类型 Equals(object) 方法

c# - WPF 4 : PropertyChanged not updating Binding

c# - 访问 WCF Web 服务

c# - WPF - 通用 IValueConverter?

c# - 在 .NET 哈希表(或其他结构)中获取最接近/下一个匹配项