How apply a custom style (from a resource) to a TextBox when it has the focus.
My usual solution
<Style TargetType="TextBox" x:Key="NoteBox">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
</Style>
Does not affect the focused or selected state.
You have to edit the Setters in its VisualState. You can get the default template here.
Just copy it and edit the values in the Focused VisualState.
Related
I am using silverlight and cant edit the header style of the data grid.
<sdk:DataGridTemplateColumn.HeaderStyle>
<Style>
<Setter Property="FontSize" Value="14" />
<Setter Property="Background" Value="Pink"/>
</Style>
</sdk:DataGridTemplateColumn.HeaderStyle>
It's written that member FontSize and Background are not accessible.
What should i do?
Try changing it to:
<sdk:DataGridTemplateColumn.HeaderStyle>
<Style xmlns:primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
TargetType="primitives:DataGridColumnHeader">
<Setter Property="FontSize" Value="14" />
<Setter Property="Background" Value="Pink"/>
</Style>
</sdk:DataGridTemplateColumn.HeaderStyle>
It should allow you set the properties with that namespace and target type added. You could also just add the namespace to the user control, instead of putting it in the style.
I have a standard TextBox with a fixed width of 150 and TextWrapping set to NoWrap, this makes the control behave just like the old WinForms version - which is what I want.
However, when I click a button, I want to effectively 'convert' the TextBox into a label. To save me messing around with multiple controls, I have decide this is best done by changing the style to look like a label. Most of this is working fine, except I want the new style to auto-resize the width of the Control to ensure all text is displayed without the need to drag a selection with the mouse.
In an attempt to do this I have set the Width to auto and then MinWidth to 150, something like this:
<Style x:Key="TypeConfusedTextBox" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Padding" Value="3"/>
<Setter Property="Height" Value="Auto" />
<Setter Property="Width" Value="Auto" />
<Setter Property="MinWidth" Value="150" />
<Setter Property="IsTabStop" Value="False"/>
</Style>
Which I then assign on button click like so:
textBox1.Style = (Style)FindResource("TypeConfusedTextBox");
The problem is that after the Style is changed, the control just remains as a fixed width of 150. If I apply the same Width and MinWidth values directly to the TextBox with the designer (or in the xaml) then is grows as expected when restyled, but so does the original style which I do not want to happen.
What am I missing here?
I'm only guessing that you're setting TextBox.Width somthing like <TextBox Width="150" .../> if that is the case then, according to Dependency Property Setting Precedence List Style won't override your fixed value. Try setting initial Width like this:
<TextBox>
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Width" Value="150" />
</Style>
</TextBox.Style>
</TextBox>
The answer by dkozl is exactly the reason why it wasn't working, and is the solution I intend to use.
However, after I posted this question I continued to try a few different things, one of which was to override the width values in code. So just to demonstrate an alternative, here is what I changed my button click code to look like:
textBox1.Style = (Style)FindResource("TypeConfusedTextBox");
textBox1.ClearProperty(WidthProperty);//this sets it to "Auto"
textBox1.MinWidth = 150;
Again, this is just an alternative, I am not recommending it as the best approach.
How would I change the background color of a TextBox Control in the Default Style Xaml to be a different color when the control is either Disabled or ReadOnly ?
You can achieve this with triggers in the style:
<TextBox>
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsReadOnly" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
Im not at a PC at the moment (just mobile) but I think you can edit the template of your control and there are some Visual States for your some controls that define things like disabled states, mouse overs, etc... which you should be able to redefine?
The way I accomplished this was to create a Converter for the control.
When the control is bound to an object it detects if the control is Enabled from this object that it is bound to. Based upon this it sets the background color for the Textbox accordingly.
I have developed a WPF Application with some buttons. Now i want to change the color of those buttons onmouseover,onmouseleave,onmouseenter by using triggers or any other events.
Any suggestion plz
Thanks in advance.
Inside the desired event, you can set the background color like this...
// Change the background color of button1 to Blue
button1.Background = Brushes.Blue;
You can also set this in a trigger:
<!-- Button will change from Blue to Yellow on MouseOver -->
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Blue" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Yellow" />
</Trigger>
</Style.Triggers>
</Style>
For even more details, check out the Property Triggers section of this article.
This question is inspired by this recent question and other situations I've encountered in my WPF development. How do I know whether it is enough to set a style on a control to override some default behavior vs creating a new control template?
More concretely, in the question above, the author wants to change the look of a ListBoxItem when it is selected. (See code reprinted below). Everything works, except the Background property. How is one supposed to know that they should override the Control Template for this?
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Content" Value="{Binding Path=Name}"/>
<Setter Property="Margin" Value="2"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontSize" Value="18"/>
<Setter Property="Background" Value="Yellow"/>
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
As to whether to use a style or template Ray provided a great response.
As to how to solve your problem without creating a template, maybe I can help.
The background color is being set by the SystemColors. Using Blend and creating a template you can see the exact xaml.
So if NO TEMPLATES! is a requirement you can always change what that resource is.
Example :
<ListBox>
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Yellow" />
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Content" Value="{Binding Path=Name}"/>
<Setter Property="Margin" Value="2"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontSize" Value="18"/>
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Resources>
<ListBoxItem>Test 1</ListBoxItem>
<ListBoxItem>Test 2</ListBoxItem>
<ListBoxItem>Test 3</ListBoxItem>
</ListBox>
That will give you the background color for that given ListBox and not screw up anything else in the app.
Styles can be thought of very closely to CSS styles in HTML. If all you want to do is change the basic properties of a control such as Background, Foreground or whatever properties it exposes then a Style is exactly what you need. Styles also allow you to apply triggers so for animations, a style is also sufficient.
If you're finding you want to change the intrinsice behaviours / inner workings on a control then a control template is what you want. For example, if you want to change how a button is laid out by adding some sort of grid behaviour, then using a control template is the way forward.
Unfortunately, for your specific example, you don't know unless you try it. Basically you first try it with a Style....and if that doesn't work for whatever reason, then you write a ControlTemplate. You usually only end up writing ControlTemplates for the reasons Ray mentioned.
My guess is that the trigger you're trying to set has also been hardcoded in the ControlTemplate...which is bad design imo because it prevents the Style from overriding it.
By "Background" I take it to mean the "blue" rectangle that surrounds the ListBoxItem when it is selected?
This is actually the FocusVisualStyle property, which is a style that describes what the item should look like when it is focused. The Control explicitly sets this property (described here), so in order to override it, you will have to redefine the Control Template, making sure to use a default Style setter to set it to {x:Null}.