override WPF styles - wpf

Looking at SharpDevelop source code I found this button declaration:
<Button Style="{x:Static core:GlobalStyles.ButtonStyle}"
Content="{core:Localize StartPage.StartMenu.NewCombineButton}"
Click="newSolutionClick" Margin="8,0,0,0" />
my problem is the style declaration: Style="{x:Static core:GlobalStyles.ButtonStyle}"
it prevents me from externally apply custom button style
if I remove the style declaration, the external theme (stored in .xaml) file works just fine.
my question: is there a way to override these specific style declarations?
Thanks a lot.
Adi Barda

You could try using BasedOn in your style... I've never used it with an x:Static so I'm not positive if it works or the syntax, but an example would be something like:
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}"
BasedOn={StaticResource core:GlobalStyles.ButtonStyle}">
<!-- Style Button here - If you define a Setter that already
exists in base style it will overwrite it -->
</Style>

Related

Apply a style to multiple controls without using a key

In .NET WPF, I have the following XAML code:
<StackPanel>
<StackPanel.Resources>
<Style TargetType="FrameworkElement">
<Setter Property="Margin" Value="5" />
</Style>
</StackPanel.Resources>
<CheckBox>Check 1</CheckBox>
<TextBox>Some text...</TextBox>
</StackPanel>
The controls do not have any margins applied to them.
Is it possible to apply a style to multiple controls (of different types) without using a key to set the style explicitly on each control?
Styles are not inherited, you can base the subclasses' styles on that one though using BasedOn.
Another method in this case should be using an ItemsControl with an ItemContainerStyle set to this style.
There are examples for both methods in this answer.
Sorry, I misread the question before I wrote this out. My answer is useful if you want to style multiple checkboxes within the StackPanel.
Implicitly style the entire application by placing this into your app.xaml's merged dictionaries.
<Style TargetType="CheckBox" BasedOn="{DynamicResource YourBaseStyle}"/>
This also works on a much smaller scope. Reducing the scope to just that StackPanel simply requires that you add that same line of code to your StackPanel.Resources tag.

Is there any way to make Style apply to all items without having to declare property

I have created a custom style for DataGrid. There are several datagrids in my application, and I would like for this style to apply to all of them.
One way of doing this is to add Style="{StaticResource DataGridDefaultStyle}" to all DataGrids.
But there must be an other way of doing this, similar to adding a theme, where I would declare style, and it would automatically apply to all DataGrids.
How do I do this?
You can use the implicit style as other have stated, but put it in your App.Xaml file's Application.Resources section, this will allow the style to be applied to all DataGrids in your application.
<Application.Resources>
<Style TargetType="DataGrid">
<Setter Property="Background" Value="LightBlue" />
</Style>
</Application.Resources>
check this Using CSS Selectors for Styling in WPF
You could try using an implicit style.
To create an implicit style, you must not include a key for the style and specify the TargetType. If you do so, the style will automatically be applied to each style of the specified type.
For example, this code will be automatically applied to all DataGrid elements:
<Style TargetType="DataGrid">
<!-- style information goes here -->
</Style>

My custom control in ToolBar

I have a specific control. I want to make style for it when it is placed in ToolBar. I have found how to do it in case of Button, CheckBox and other standard controls, but how I should make it for my control?
<Style x:Key="MyStyleForCustomControl" TargetType="{x:Type NameSpace:CustomControl}">
// Your setter's for your controls go here.
</Style>
NameSpace - where your Control is present
CustomControl - Name of your control.
Is this what you want?
EDIT:
If you want Style for Toolbar in your control, simply place the style in your UserControl Resources, it will be applied to the Toolbar placed within your control. The scope for this style will be limited to your control and will be hidden outside your control.
<UserControl.Resources>
<Style TargetType="{x:Type ToolBar}">
....
</Style>
</UserControl.Resources>
Ok now you clarified your question I think I can give you a solution. It looks like you simply need to set the ItemContainerStyle of the ToolBar control for your specific user control. First declare the "my" namespace where your specific control is located, then just add something like this:
<ToolBar>
<ToolBar.ItemContainerStyle>
<Style TargetType="{x:Type my:MyUserControl}">
<Setter Property="Background" Value="Azure"/>
</Style>
</ToolBar.ItemContainerStyle>
<my:MyUserControl/>
</ToolBar>
If you wanna add other control types to the ToolBar like the Button you mentioned above, then you will need to define a custom StyleSelector instead that you will set to the ItemContainerStyleSelector property. Here is a pretty good sample of StyleSelector implementation: Style Selectors

Style controls in Silverlight - global

Is there any way to create "global" styles for all TextBoxes used in application? So I don't have to do Template=... on each TextBox ?
Yes. You need to use an implicit style.
Declare the style as normal:
<Style x:Key="MyDefaultStyle" TargetType="TextBox">
....
</Style>
Then declare:
<Style TargetType="TextBox" BasedOn="MyDefaultStyle />
The important part is that you are missing the x:Key declaration. This will get applied to all TextBoxes in your application that don't explicitly set a Style. Include this in a global resource file.
There's more information on SilverlightShow.net.

WPF Grid.Resources Style breaks ResourceDictionary Style. How to make them coexist?

In a WPF app I have a ResourceDictionary with Style defined for the TargetType MyCustomControl:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cc="clr-namespace:MyControlLibrary;assembly=MyControlLibrary" >
<Style TargetType="{x:Type cc:MyCustomControl}">
<Setter Property="Prop1" Value="1" />
<Setter Property="Prop2" Value="2" />
...
The problem is that I need to define a context menu for MyCustomControl in the page XAML like following:
<Grid>
<Grid.Resources>
<ContextMenu x:Key="MyControlContextMenu">
<MenuItem Name="Name1"
Header="Header1"
Click="Cm1_Click"
.../>
....
</ContextMenu>
<Style TargetType="{x:Type ScNamespace:MyCustomControl}">
<Setter Property="ContextMenu" Value="{StaticResource MyControlContextMenu}"/>
</Style>
</Grid.Resources>
...
In this case, though I only assign one Property in the Style definition inside my Grid, the ResourceDictionary Style values are not applied at all. They get overridden by page resource style and ignored.
How to solve this issue? Maybe there is a way to make a reference to ResourceDictionary in the Grid.Resources section to enforce looking up the ResourceDictionary Style?
Base your new style on your default style:
<Style TargetType="{x:Type ScNamespace:MyCustomControl}" BasedOn="{StaticResource {x:Type ScNamespace:MyCustomControl}}">
<Setter Property="ContextMenu" Value="{StaticResource MyControlContextMenu}"/>
</Style>
Not sure if it is what you're looking for, but a Style can inherit from another. With the BasedOn-property you can define the base style of a Style, so that the new style inherits all settings from this style.
However I never tried if it works also if the BasedOn references to the same key (type). Maybe it works:
<Style TargetType="{x:Type ScNamespace:MyCustomControl}"
BasedOn="{x:Type ScNamespace:MyCustomControl}">
If this works not, maybe you can separate the Style, define it globaly with a key and then reference to the globaly defined Style via the BasedOn-property.
In general, Controls should have their default Styles defined in the Themes folder in a theme specific file (see here for more info).
When an application looks for a
resource, it looks at three levels in
the following order:
1) The element level.
The system starts with the element
that references the resource and then
searches resources of the logical
parent and so forth until the root
element is reached.
2) The application level.
Resources defined by the Application
object.
3) The theme level.
Theme-level dictionaries are stored in
a subfolder named Themes. The files in
the Themes folder correspond to
themes. For example, you might have
Aero.NormalColor.xaml,
Luna.NormalColor.xaml,
Royale.NormalColor.xaml, and so on.
You can also have a file named
generic.xaml. When the system looks
for a resource at the themes level, it
first looks for it in the
theme-specific file and then looks for
it in generic.xaml.
In your case, you have two implicit Styles, so HCL and Kent's answers should work. Since only one implicit Style can be applied at a time. Same goes for setting the Style properly directly. In that case, no implicit Styles will be applied.
If you have your first Style setup as a default Style at the theme level, then it would be applied in addition to your second implicit Style (or any explicitly defined Style).

Resources