0

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> 

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

No, it does not work for me both on win7 & win server 2003 sp2. I tried another controls listbox&datagrid, for listbox i only style the listboxitem's highlighting, for datagrid with styling the datagridrow's highlighting, either of them work, unless the control template created. I think the control template need more basic functions that's unnecessary at all. Are there more settings for the control to 'enable' the "styling mode"?
WPF is pretty much going to do the same thing on any OS - it's the version of WPF that will make the difference, and as far as I can see this is working on all versions of the framework I try and build for - I updated the answer - can you try that code in your project
It works for simply changing the background rather than the border brush. "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.", but how do you did this? There's no default animation for you?
1

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> 

1 Comment

yes, I did it. Here my border's thickness is 1. Please see the textbox's code i updated just now.
1

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).

1 Comment

yes, changing background works! As you said, it's the default animation supress my style of border. Thank you very much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.