WPF:控件的样式不起作用,除非为该样式创建了控件模板 [英] WPF: the style of control does not work unless the control template created for style

查看:230
本文介绍了WPF:控件的样式不起作用,除非为该样式创建了控件模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近几天我几乎发疯了.我在xaml文件中有一个文本框和样式. 但是没有控件模板的样式不能在文本框上生效.尽管控件模板可以工作,但是控件模板似乎完全覆盖了文本框,但是默认行为是丢失了文本框,例如编辑,输入或选择... 这是带有控件模板的xaml的内容:

It almost gets me mad in recent days. I have a textbox and the style in xaml file. But the style without a control template cannot take effect on textbox. Whereas, a control template works, but control template seems to overwrite the textbox totally, the default behaviors loses of textbox such as editing, inputing or selecting... Here is content of xaml with the control template:

   <Style TargetType="{x:Type TextBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border Name="tbBorder"  Background="White"  BorderThickness="0.6" BorderBrush="#B9B9B9">
                        <ContentPresenter/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="true">
                            <Setter Property="BorderBrush" Value="#4D90FE" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="BorderBrush" Value="#4D90FE" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

这是根本不起作用的简单样式,

And here is the simple style which does not work at all,

    <Style TargetType="{x:Type TextBox}">
       <Style.Triggers>
            <Trigger Property="IsFocused" Value="true">
                <Setter Property="BorderBrush" Value="#4D90FE" />
            </Trigger>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="BorderBrush" Value="#4D90FE" />
            </Trigger>
        </Style.Triggers>
    </Style>

谢谢!

更新:整个文本框的代码段:

update: the entire textbox's code snipt:

       <TextBox Height="23" HorizontalAlignment="Left" Margin="114,53,0,0" Name="textBox1" VerticalAlignment="Top" Width="150" Text="{Binding Path=TraderAccount, Mode=OneWayToSource, NotifyOnValidationError=True}" BorderBrush="#FFB9B9B9" BorderThickness="1" >
            <TextBox.Style>
                <Style TargetType="{x:Type TextBox}">
                    <Style.Triggers>
                        <Trigger Property="IsFocused" Value="true">
                            <Setter Property="BorderBrush" Value="Red" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="BorderBrush" Value="Red" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>

推荐答案

样式设置器对我有用,但是我看到的问题是控件动画使刚刚设置的样式动起来了.

The style setter is working for me but the issue I see is that the controls animations are animating away the style that's just been set.

您可能想要提取原始控件模板并重新定义它,而不是完全重新定义它.据我所知,文本框控件比带有内容演示者的边框要复杂得多(尽管我从未为其提取控件模板!),并且它可能具有几个边框,可以使它呈现所有状态等等

You may want to extract the original control template and redefine that rather than completely redefining it. As far as I know The textbox control is more complex than just a border with a content presenter (I've never extracted the control template for it though!) and its likely to have a couple of borders that work to give it all it's states etc

您可以使用Blend来做到这一点-在没有Blend的情况下,有用于控件模板和样式的MSDN资源:

You can use Blend to do this - in the absence of Blend there is the MSDN resource for control templates and styles:

http://msdn.microsoft.com/en-us/library/aa970773.aspx

对于初学者来说,我看起来好像您在重新定义的模板中缺少内容"PART"

For starters it looks to me like you are missing the content 'PART' in your redefined template

<ScrollViewer Margin="0" x:Name="PART_ContentHost" />

您是说它不起作用...这对使用.NET Framework 4.0的WPF来说对我有效-我将边框颜色更改为红色",以确保可以看到效果,并且肯定可以使用立即从红色褪色中消失,因为控件的视觉状态已由Visual State Manager更改(这就是为什么您需要编辑控件模板并更改视觉状态的原因)

You are saying it doesn't work... this works for me on WPF using .NET Framework 4.0 - I changed the border colour to 'Red' instead to make sure I could see the effect and it definitely works, aside from the red fading immediately because the controls visual state is changed by the Visual State Manager (which is why you need to edit the control template and change the visual states)

<TextBox>  
    <TextBox.Style>  
        <Style TargetType="TextBox">  
            <Style.Triggers>  
                <Trigger Property="IsFocused" Value="true">  
                    <Setter Property="BorderBrush" Value="Red" />  
                </Trigger>  
                <Trigger Property="IsMouseOver" Value="true">  
                    <Setter Property="BorderBrush" Value="Red" />  
                </Trigger>  
            </Style.Triggers>  
        </Style>  
    </TextBox.Style>  
</TextBox>  

将鼠标悬停在框上时,会出现一个红色边框,该边框会立即消失

When you hover over the box, you get a red border which immediately fades

此XAML根本不适合您吗?

Does this XAML not work for you at all??

这篇关于WPF:控件的样式不起作用,除非为该样式创建了控件模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆