How to use StyleSelector without losing the global style set? - wpf

lets say that I have a global style set for ItemsContainer control, and this style is applied well during runtime. Now I have a requirement to paint Row background for each row, based on some condition. For this I decided to use RowStyleSelector.
<RadTreeListView RowStyleSelector="{StaticResource ActivityRowStyleSelector}" />
<Style x:Key="xxxTreeListViewStyle" TargetType="telerik:TreeListViewRow">
<Setter Property="Background" Value="#FFFFE1C4" />
</Style>
<local:xxxRowStyleSelector x:Key="xxxRowStyleSelector"
FailureStyle="{StaticResource xxxTreeListViewStyle}"
.../>
But this overrides the global style that I set for row, and I just want to change Background property to existing style.
And another question is: how would I apply this change to a particular cell, not row?

You can use the Style.BasedOn property to "inherit" one style from another. Use it like this (where RowGlobalStyle is the name of the global style you've created previously):
<Style x:Key="xxxTreeListViewStyle" TargetType="telerik:TreeListViewRow"
BasedOn="{StaticResource RowGlobalStyle}">
<Setter Property="Background" Value="#FFFFE1C4" />
</Style>

Related

Not all styles are overwritten in my WPF controls

I have a system where I use my WPF UI as a class library. I also use different usercontrols to control what the user see during the span of the UIs life cycle. These are all set in code. I then added a theme system where you could create a XAML ResourseDictionary file and use that to change the look on the UI. To apply the style, everytime I create the window or a user control, that FrameworkElement goes through this code:
public void ApplyStyle(FrameworkElement element)
{
var targetDir = element.Resources.MergedDictionaries.FirstOrDefault(d => d.Contains("SonaStyleDocument"));
if (targetDir != null && loadedResource != null)
{
element.Resources.MergedDictionaries.Remove(targetDir);
element.Resources.MergedDictionaries.Add(loadedResource);
}
}
In my view I do set a standard style document via the xaml code. I locate the default style in the code, remove it and add the new one. Now this works, and I can see the changes when I apply a external ResourceDictionary. However, for some reason my buttons does not react to the changes. They keep the same styles, even though it has been removed. Here is one of my buttons code:
<Button Style="{StaticResource KeyboardToggleButton}" Command="{Binding KeyboardToggleCommand}">
<Button.Content>...</Button.Content>
</Button>
It uses the style KeyboardToggleButton, which has the follow style in my default style document:
<Style TargetType="Button" x:Key="SonaButton">
...
</Style>
<Style TargetType="Button" x:Key="SonaPrimaryButton" BasedOn="{StaticResource SonaButton}">
<Setter Property="Template" Value="{StaticResource DxcPrimaryButtonTemplate}" />
...
</Style>
<Style TargetType="Button" x:Key="ManipulationButton" BasedOn="{StaticResource SonaPrimaryButton}">
...
</Style>
<Style TargetType="Button" BasedOn="{StaticResource ManipulationButton}" x:Key="KeyboardToggleButton" />
But I replace it with the following style(s):
<Style TargetType="Button" x:Key="SonaButton">
<Setter Property="Template" Value="{StaticResource RoundCornerTemplate}"></Setter>
</Style>
<Style TargetType="Button" x:Key="ManipulationButton" BasedOn="{StaticResource SonaButton}">
...
</Style>
<Style TargetType="Button" x:Key="KeyboardToggleButton" BasedOn="{StaticResource ManipulationButton}"/>
What could be going on here? They both have the same key for KeyboardToggleButton, though the templating is different and I can see that my other controls react fine to the new styles they get. I even tested just adding the new style and let the old one remain and that has the same result. The buttons just won't accept the new style.
Update:
So I was reading various articles about styles to see if I find some more information, and some article mentioned Dynamic resources. To isolate the problem I first remade my default style of one of my buttons to be very basic:
<Style TargetType="ButtonBase" x:Key="KeyboardToggleButton" />
And then changed the style I want to overwrite the default style, to just change the background:
<Style TargetType="Button" x:Key="KeyboardToggleButton">
<Setter Property="Background" Value="Yellow"/>
</Style>
With this setup nothing happened, but when I changed the Style from a StaticResource to DynamicResource, then it worked. However, this presented another problem. I tried testing this result on one of my other buttons with all substyles and control templates activated and the system throwsan exception in the code that adds the style:
System.InvalidOperationException: 'Property can not be null on Trigger.'
The style I try to use instead of the default style has a control template with triggers, to handle hover events, click event and more. My theory at this point is that I have had this problem also with the StaticResource, but the error is hidden and the system defaults directly to the old style. My issue with this theory is that I actively remove the default style before I enter in the new. Not removing the old style does not help.
I found the problem. The exception was indeed the answer, but only when I changed the code to apply each style individually. I should have followed the exception a bit more, but I had tunnel vision. The answer came from here: Why is the WPF property IsDefaulted not found?
I have first tried to change to buttonbase, but that does not work with the trigger "IsDefaulted". That will not resolve and the property will be null. Changing back to TargetType="Button" worked. No more exceptions. Then I also changed all style references to be dynamic and now all buttons (Except one, which I am looking into) have the correct style. It makes sense that it should be Dynamic as I change it during runtime.

TargetType does not work on generated Controls?

What I try to achieve is to set a style of a, on runtime generated, button automatically. So I would like to do it without using:
dirButton.Style = (Style)Application.Current.FindResource("DirectoryStyle");
This is my style I defined in my App.xaml (I deleted the setters for display purposes):
<Style x:Key="DirectoryStyle" TargetType="{x:Type local:DirectoryButton}">
<Setter Property="OverridesDefaultStyle" Value="true"/>
</Style>
The Button stated above is generated on start up and stands for one folder within a given directory.
Directory Button dirButton = new DirectoryButton();
dirButton.Click += directoryButton_Click;
// Here I could set the style with the first code segment
So in short:
Is there a way to make TargetType affect generated controls?
Remove the x:Key from the Style declaration to make it a default Style for the TargetType.
<Style TargetType="{x:Type local:DirectoryButton}">
...
</Style>

WPF - Global Style?

Is there a way to setup global styles for my WPF application?
What I'm hoping to do is apply a style to all my Buttons that also have an Image child.
Well, sort of - it's a catch-all approach you can do - put the following element in your App.xaml - all your buttons will change (except the ones you apply a style to, manually).
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="LightPink"/> <!-- You should notice that one... -->
</Style>
However, if you want to hit only buttons with images - you have to inherit from Button everytime you do and then apply a style like this:
public class CustomImageButton:Button{}
<Style TargetType="{x:Type local:CustomImageButton}">
<Setter Property="Background" Value="LimeGreen"/>
</Style>
<local:CustomImageButton Content="ClickMe"/>
It is a very coarse-grained global styling - and you need to follow the convention to make it work.
An alternative is to use Themes - read more about that here.
You can do implicit styles in WPF which are applied by type
for instance
<Style TargetType="Button">
Will be applied to ALL the buttons within the scope of that style (If the style is in App.XAML it will apply to all buttons, if it is lower in the chain it will apply to all buttons underneath it)
If you want to apply it to only certain types of buttons (say imagebuttons) create a type that derives from button (call it ImageButton) and then create a style targeted to that type.
Put the style into a ResourceDictionary tag inside your App.xaml and it will apply to the entire app.

WPF Menu Items Styles

I have an application resource of the following
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="{DynamicResource windowTextBackColor}"/>
<Setter Property="Foreground" Value="{DynamicResource windowsTextForeColor}"/>
</Style>
So all the text blocks in my application should assume those colours.
However the Menu and its containing MenuItems on my Main Window does not take these colours?
I have to do the XAML
for it to assume those colours, Is there a reason why setting a style that targets Text blocks does not work?
Thanks
I think you have to style the menu and menuitems separately. A MenuItem is a HeaderedContentControl, and its Header property is not a TextBlock, but an object, so it wouldn't be affected by a style for TextBlock.
You might also try changing that style to target Control instead of TextBlock. (Control is where Foreground and Background are defined.) I can't say for sure that it'll work, but if it does, it'll make every Control (TextBlocks, MenuItems, Buttons...) have those background and foreground colors.
Also, you might consider using BasedOn so that you can "inherit" the styles. If you don't, then styles defined farther up the hierarchy won't affect controls that have a style defined lower in the hierarchy. Basically, the lower ones mask the higher ones, unless you used BasedOn. Use it in this fashion:
BasedOn="{StaticResource {x:Type <your type here>}}"

How to use MultiDataTrigger to check a single condition to be true in Style.Triggers in WPF?

I have three grids in my UserControl of which one control is shown at time. In the last column I need to use a Style where I need to check the data and apply a ForeGround color. I can write style at each of the control in 3 grids using DataTriggers. But I want a concrete style in Resource which can be used anywhere. I tried MultiDataTrigger but it doesn't serve my purpose as it checks 2 or more Condintions to be true in MultiDataTrigger.Conditions whereas i need to check data in a single control. Are there any alternate solution to achieve this?
If you're using some kind of a grid, you're probably using CellTemplate or some other property like that to accomplish your task. I think you do need to use different styles in different columns.
But if those styles are the same except for the triggers, then you can make one style with everything that's common to both of them, and then create another style based on the first one. It's a bit similar to inheritance in OOP.
This is how it may look like:
<Style x:Key="BaseStyle" TargetType=".....">
<!-- Common setters and triggers -->
<Setter ... />
<Setter ... />
<Setter ... />
</Style>
<Style x:Key="InheritedStyle" BasedOn="{StaticResource BaseStyle}" TargetType=".....">
<!-- This style's specific setters and triggers -->
<Setter ... />
<Style.Triggers>
...
<Style.Triggers>
</Style>

Resources