简体   繁体   English

为什么我的文本框对ControlTemplate的样式中的IsMouseOver没有反应

[英]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. 因此,我尝试对TextBox进行一些样式设置,其想法是,我想在将鼠标悬停在TextBox上时对BorderBrush进行样式设置,但由于某种原因它没有更改边框,因此我也尝试更改了Background, 。 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. 那时我意识到它对IsMouseOver没有反应,但确实设置了背景以及我所做的所有其他事情,只是事件IsMouseOver没有反应。

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 : 您缺少TargetNameBorderThickness

<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 . 没有Border除非您将其BorderThickness属性设置为0以外的0

You are using a custom Template , but your template does not apply the border properties of the templated TextBox . 您使用的是自定义Template ,但是您的模板未应用模板化TextBox的border属性。 Try adding the following to the Border element in your template: 尝试将以下内容添加到模板的Border元素中:

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 ; 实际上,您可以通过几种方法进行处理:使用模板绑定并将触发器放入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? 例如:如果有人直接在TextBox上设置了新的BorderBrush ,您是否希望它取代您的橙色悬停效果? 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. TextBox希望其模板具有一个专门命名的ScrollViewer ,可以在其中插入可编辑的文本视图。 Try replacing your ContentPresenter with the following: 尝试将您的ContentPresenter替换为以下内容:

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

Note that the x:Name attribute must match exactly. 请注意, x:Name属性必须完全匹配。

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

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