简体   繁体   中英

Why doesn't my Textbox react on IsMouseOver in the Style for the ControlTemplate

So I am trying to style my TextBox a little bit, the idea was that I wanted to style the BorderBrush when hovering over the TextBox but for some reason it didnt change the Border, so I tried changing the Background as well, and same thing there. That's when I realized it doesnt react to the IsMouseOver but it does set the background and everything else that I did, it's just the event IsMouseOver that it's not reacting to.

The Control

<TextBox Width="700"
         Height="340"
         HorizontalAlignment="Right"
         Margin="0,0,230,140"
         Foreground="#8bf502"
         TextWrapping="Wrap"
         Style="{DynamicResource TextboxStyle}"/>

The Style

<Style TargetType="{x:Type TextBox}" x:Key="TextboxStyle">
    <Setter Property="OverridesDefaultStyle" Value="True"></Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <Border Background="#424242">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="BorderBrush" Value="Orange"></Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

要更改边框的背景,请给边框命名,然后将SetName =“ MyBorder”添加到设置器中

You are missing a TargetName and a BorderThickness :

<Style TargetType="{x:Type TextBox}" x:Key="TextboxStyle">
    <Setter Property="OverridesDefaultStyle" Value="True"></Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <Border Name="border" BorderThickness="2" Background="#424242">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="border" Property="BorderBrush" Value="Orange"></Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

There is no Border unless you set the BorderThickness property of it to something else than 0 .

You are using a custom Template , but your template does not apply the border properties of the templated TextBox . Try adding the following to the Border element in your template:

BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"

There's actually a few ways you can approach this: use template bindings and put the trigger in the Style ; use template bindings and put the trigger in the template; use an explicit target element and put the trigger in the template (as others here have suggested); etc. The "right" approach depends on how/if you want the control to respond when the property is changed. For example: if someone sets a new BorderBrush directly on the TextBox , do you want that to supersede your orange hover effect or not? It's worth reading up on value precedence of dependency properties to understand the implications of how and where certain properties are set.

It's worth noting that your template is incomplete and won't work if you want the text to be editable (or selectable). A TextBox expects its template to have a specially named ScrollViewer where an editable text view can be injected. Try replacing your ContentPresenter with the following:

<ScrollViewer x:Name="PART_ContentHost"
              Padding="{TemplateBinding Padding}" />

Note that the x:Name attribute must match exactly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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