<Style x:Key="originalStyle" TargetType="TextBox">
...setters...
<Style.Triggers>
<DataTrigger Binding="{Binding yyy}" Value="1">
<Setter Property="FontSize" Value="{DynamicResource xxx}"/>
</DataTrigger>
</Style.Triggers>
</Style>
I like everything in the Style except for the DataTrigger which I want to remove. How can change it?
<Style x:Key="derivedStyle" BasedOn="{StaticResource originalStyle}">
...How to remove the DataTrigger???....
</Style>
You could make a "base style" containing all the common stuff, and create two styles BasedOn that style.
Related
I am trying to use a setter in my application to change to font size of all Controls. My Style is looking like this:
<Style x:Key="baseStyle" TargetType="{x:Type Control}">
<Setter Property="FontSize" Value="12" />
<Setter Property="Background" Value="{x:Null}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Width, ConverterParameter=1000, Converter={StaticResource DoubleToBoolConverter}, ElementName=window, Mode=OneWay}" Value="True">
<Setter Property="FontSize" Value="24" />
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
All of my substyles are looking like this
<Style BasedOn="{StaticResource baseStyle}" TargetType="{x:Type Button}">
...
</Style>
My problem is that my styles isnot applied to all controls. Labels for example seam to ignore my FontSize Setter
Default Style:
Triggered Style:
Do you actually refer to Label elements or TextBlock elements? Because the latter type is not a Control and won't be affected by your Style. This should work though:
<Style BasedOn="{StaticResource baseStyle}" TargetType="{x:Type Label}" />
But please always remember to provide a reproducible sample of your issue when asking a question: https://stackoverflow.com/help/mcve
I have a toolbarpanel which I've made a custom style.
<Window.Resources>
<Style x:Key="toolbar_opciones" TargetType="{x:Type ToolBarPanel}">
<Setter Property="Background" Value="DeepSkyBlue"/> <!-- does not work -->
<Setter Property="Cursor" Value="Hand"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="CadetBlue"/>
</Trigger> <!-- does not work -->
</Style.Triggers>
</Style>
</Window.Resources>
<ToolBarPanel Style="{StaticResource toolbar_opciones}"/>
The cursor works but the background property no, why?
I tested your code, and here is the fix
<ToolBarPanel Style="{StaticResource toolbar_opciones}"/>
look at it closely, you were missing the double quotation which close the Style attribute. When I added it to the XAML, the Background has been applied.
Cheers.
I am working on a c# WPF project which I am using a style trigger to style the background colour of the each row based on one of the cell values.
Below is my style trigger.
<Window.Resources>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Highest Alarm Level}" Value="Critical">
<Setter Property="Background" Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding Highest Alarm Level}" Value="Medium">
<Setter Property="Background" Value="Orange" />
</DataTrigger>
<DataTrigger Binding="{Binding Highest Alarm Level}" Value="Warning">
<Setter Property="Background" Value="Yellow" />
</DataTrigger>
<DataTrigger Binding="{Binding Highest Alarm Level}" Value="Info">
<Setter Property="Background" Value="White" />
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
What I need to be able to do, is under certain circumstances I don't want this style to be used, so what I am after, is if in the C# code a certain condition becomes to true, I want to disable all of the styling done above to not be used, i.e. all of the styling is turned off so the background colour of the rows are not set.
Add a Key to your custom DataGridRow Style
<Style TargetType="DataGridRow" x:Key="MyRowStyle">
<!-- Define Triggers -->
</Style>
Then you just need to switch between default and custom styles.
If you set Style with null, that means the default Style will be applied
dataGrid.RowStyle = _boolCondition ? this.FindResource("MyRowStyle") as Style : null;
I had make an Button Style as DataTemplate in a ResourceDictionary. Here a small part:
<Style TargetType="{x:Type Button}">
<Setter Property="Focusable" Value="False"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border">
...
</Border>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected}" Value="True">
<Setter Property="Background" TargetName="border" Value="Red" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
In this Template a have a binding to a Propertie IsSelected. This Propertie is in one situation there and in a other not.
Is it possible to Check in Xaml if the binding path exist, then use it in other case forget it?
Now i had BindingExpression in the Debug output and i want to eliminate this.
The more pertinent question is: why do you have a DataTrigger in your ControlTemplate? This creates a dependency between the control and its data context, which is why you're experiencing this issue when your data context does not match the control template's expectations.
Are you certain you cannot use a more suitable mechanism? For example, could you instead use a style for those buttons where IsSelected should affect the Background?
<Style x:Key="SpecialButtonStyle" TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected}" Value="True">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
...
<Button DataContext="relevant data context" Style="{StaticResource SpecialButtonStyle}"/>
Or, even better, could you define a data template for the specific data class you have that has the IsSelected property? This data template could automatically use the correct Button style.
Does anyone know how to style the background property of a WPF ComboBox when a mouse is hovering on top of it?
I cannot get rid of the blue-ish button like background off the ComboBox.
You can style it like anything else:
<Style TargetType="{x:Type ComboBox}" x:Key="HoverBox">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
usage:
<ComboBox Style="{StaticResource HoverBox}" ... />
And at the top of your UserControl/ Window you have to place the style:
<UserControl...>
<UserControl.Resources>
<Style TargetType="{x:Type ComboBox}" x:Key="HoverBox">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
[CONTENT HERE]
</UserControl>
It will not work.Its because of the default control template of ComboBox.You may need to override the default template for this behavior.Have a look at
MouseOver highlighting style returning to default after a second (Caused by Aero?)
http://social.expression.microsoft.com/Forums/en/blend/thread/b210978c-24e8-431b-916b-a40a752b990c
http://social.msdn.microsoft.com/Forums/en/wpf/thread/a18891e9-8879-4819-9679-247341782f60