简体   繁体   English

具有自定义样式的文本框不会触发GotFocus

[英]TextBox with Custom Style not firing GotFocus

I have what to some may seem like a simple problem and despite my best efforts, I have been unable to 'fix' this problem: 我有一些似乎很简单的问题,尽管尽了最大的努力,但我仍无法“解决”此问题:

I have a custom style for a TextBox, defined in a 'common' assembly; 我有一个TextBox的自定义样式,在“通用”程序集中定义; this style overrides the default and is thus used every time someone uses a TextBox. 此样式将覆盖默认样式,因此每次有人使用TextBox时都会使用此样式。 The problem is that when the style is applied, the GotFocus, IsKeyboardFocusWithinChanged and PreviewGotKeyboardFocus events are -not- fired any longer. 问题在于,应用样式时,不再触发GotFocus,IsKeyboardFocusWithinChanged和PreviewGotKeyboardFocus事件。 I've verified that this only happens when this style is applied (If I comment it out and run the application, the events are fired correctly). 我已经证实只有在应用此样式时才会发生这种情况(如果我注释掉并运行该应用程序,则事件将正确触发)。

So my question is essentially, has anyone experienced anything similar? 所以我的问题本质上是,有人经历过类似的经历吗? And if so, does anyone know of a solution for this problem? 如果是这样,有人知道这个问题的解决方案吗?

The style is as follows (The static resources a simple SolidColorBrushes): 样式如下(静态资源简单的SolidColorBrushes):

<Style TargetType="{x:Type TextBox}" x:Key="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="BorderBrush" Value="{StaticResource BrushBorder}"/>
    <Setter Property="ContextMenu" Value="{StaticResource TextBoxContextMenu}" />
    <Setter Property="Foreground" Value="{StaticResource BrushForeground}" />
    <Setter Property="SelectionBrush" Value="{StaticResource BrushHighlight}" />
    <Setter Property="Background" Value="{StaticResource BrushBackground}"/>
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border BorderThickness="1" x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}">
                    <ScrollViewer x:Name="PART_ContentHost"
                                  Margin="0" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsKeyboardFocusWithin" Value="True">
            <Setter Property="Background" Value="{StaticResource BrushLightBg}" />
        </Trigger>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="{StaticResource BrushMouseoverBackground}" />
        </Trigger>
    </Style.Triggers>
</Style>

Hi thi is working just fine here, I had to add some colors you were missing, which will make your eyes bleed pardon that, but I'm in a bit of a rush here :) 嗨,我在这里工作得很好,我不得不添加一些您缺少的颜色,这会让您的眼睛流血,但是,我有点着急:)

<!-- nice colors you were missing them ;) -->
<SolidColorBrush x:Key="BrushBorder" Color="Blue"/>
<SolidColorBrush x:Key="TextBoxContextMenu" Color="DeepPink"/>
<SolidColorBrush x:Key="BrushForeground" Color="Red"/>        
<SolidColorBrush x:Key="BrushHighlight" Color="Aqua"/>
<SolidColorBrush x:Key="BrushBackground" Color="DarkBlue"/>
<SolidColorBrush x:Key="BrushLightBg" Color="LightSeaGreen"/>
<SolidColorBrush x:Key="BrushMouseoverBackground" Color="Yellow"/>

<Style TargetType="{x:Type TextBox}" x:Key="TextBoxStyleMessed" BasedOn="{StaticResource {x:Type TextBox}}" >
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="BorderBrush" Value="{StaticResource BrushBorder}"/>
    <!--<Setter Property="ContextMenu" Value="{StaticResource TextBoxContextMenu}" />-->
    <Setter Property="Foreground" Value="{StaticResource BrushForeground}" />
    <Setter Property="SelectionBrush" Value="{StaticResource BrushHighlight}" />
    <Setter Property="Background" Value="{StaticResource BrushBackground}"/>
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border BorderThickness="1" x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}">
                    <ScrollViewer x:Name="PART_ContentHost" Margin="0" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsKeyboardFocusWithin" Value="True">
            <Setter Property="Background" Value="{StaticResource BrushLightBg}" />
        </Trigger>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="{StaticResource BrushMouseoverBackground}" />
        </Trigger>
    </Style.Triggers>
</Style>

.....

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <TextBox Text="Hello!" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{DynamicResource TextBoxStyleMessed}" 
             x:Name="HateThisNonMVVMStuff"/>
    <Button Grid.Row="1">Hodwy</Button>
</Grid>

Had to cut some corners but nasty codebehind: 不得不偷工减料,但背后隐藏着讨厌的代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Loaded += MainWindow_Loaded;
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        HateThisNonMVVMStuff.PreviewLostKeyboardFocus += HateThisNonMVVMStuff_PreviewLostKeyboardFocus;
        HateThisNonMVVMStuff.LostFocus += UIElement_OnLostFocus;
        HateThisNonMVVMStuff.GotFocus += UIElement_OnGotFocus;

    }

    void HateThisNonMVVMStuff_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
        System.Diagnostics.Trace.WriteLine("PrevLostFocus!");
    }

    private void UIElement_OnGotFocus(object sender, RoutedEventArgs e)
    {
        System.Diagnostics.Trace.WriteLine("Hei! Gained focus");
    }

    private void UIElement_OnLostFocus(object sender, RoutedEventArgs e)
    {
        System.Diagnostics.Trace.WriteLine("Hei! Lost focus");        
    }
}

The events fires with your style, I applied it like a style for this one box only. 这些事件随您的样式而变化,我仅将此样式应用于此框。 I got an exception on your contextmenu and that it wouldn't have my nasty color, but hey, who would! 我的上下文菜单中有一个例外,它没有我讨厌的颜色,但是,嘿! :D :d

Hope it helps, 希望能帮助到你,

Cheers, 干杯,

Stian 了Stian

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM