具有自定义样式的TextBox不会触发GotFocus

时间:2014-09-02 11:28:43

标签: c# wpf textbox styles controltemplate

我有些看似简单的问题,尽管我付出了最大的努力,但我还是无法解决问题。这个问题:

我有一个TextBox的自定义样式,定义在' common'部件;此样式会覆盖默认值,因此每次有人使用TextBox时都会使用此样式。 问题是,当应用样式时,GotFocus,IsKeyboardFocusWithinChanged和PreviewGotKeyboardFocus事件不再被触发。 我已经验证这只会在应用此样式时发生(如果我将其注释掉并运行应用程序,则会正确触发事件)。

所以我的问题基本上是,有没有人经历过类似的事情?如果是这样,有没有人知道这个问题的解决方案?

样式如下(静态资源简单的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>

1 个答案:

答案 0 :(得分:2)

嗨,你在这里工作得很好,我不得不添加你失踪的一些颜色,这会让你的眼睛流血赦免,但我在这里有点匆忙:)

<!-- 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>

不得不削减一些角落但讨厌的代码隐藏:

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");        
    }
}

事件随着你的风格而爆发,我把它应用为这个盒子的风格。我在你的情境菜单上有一个例外,它不会有我讨厌的颜色,但嘿,谁会! :d

希望它有所帮助,

干杯,

了Stian