WPF: the style of control does not work unless the control template created for style

老子叫甜甜 提交于 2019-12-25 05:22:24

问题


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>

Thanks!

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>

回答1:


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

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

Edit:

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

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

Edit 2:

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

Does this XAML not work for you at all??




回答2:


You did not post TextBox code but I assume (it happened to me too) that you simply forgot to set BorderThickness of your textbox:

<TextBox BorderThickness="4">
        <TextBox.Style>
            <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>
        </TextBox.Style>
    </TextBox>



回答3:


Your style does work, set a property like Background in your style without the template and you will see that it does get applied. However, like someone else mentioned, the reason you do not see any changes is because of animation in the default WPF control template for TextBox (Animation values always take precedence over local values, setters and triggers). When you redefine the control template, those animations are no longer there and so your example works. What you could do is take the default TextBox template and modify it to suit your purposes (can be found here: http://msdn.microsoft.com/en-us/library/cc645061%28VS.95%29.aspx).



来源:https://stackoverflow.com/questions/12796916/wpf-the-style-of-control-does-not-work-unless-the-control-template-created-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!