Style vs. ControlTemplate - wpf

is it possible to define resources in the style rather then using a template?
<ListView.Resources >
<Style TargetType="{x:Type ScrollBar}">
<Setter Property="Background" Value="Transparent" />
</Style>
</ListView.Resources>
How can I wrap this thing into:
<Style TargetType="{x:Type ListView}">
</Style>
?

I'm not sure I understand your question correctly... is that what you're looking for ?
<Style TargetType="{x:Type ScrollBar}">
<Style.Resources>
<Style TargetType="{x:Type ListView}">
</Style>
</Style.Resources>
<Setter Property="Background" Value="Transparent" />
</Style>

Related

How do I apply styles to different `TextBlock` objects?

Here is my Application ResourceDictionary
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CABI_PO_Manager.Themes">
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="FontFamily" Value="Comic Sans MS"/>
<Setter Property="FontSize" Value="14"/>
</Style>
<Style BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="TextBlock"
x:Key="YellowTextBlock">
<Setter Property="FontSize" Value="16"/>
<Setter Property="Foreground" Value="#d8b243"/>
</Style>
<Style BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="TextBlock"
x:Key="GreenTextBlock">
<Setter Property="FontSize" Value="18"/>
<Setter Property="Foreground" Value="Green"/>
</Style>
<Style BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="TextBlock"
x:Key="RedTextBlock">
<Setter Property="FontSize" Value="14"/>
<Setter Property="Foreground" Value="#a01e21"/>
</Style>
</ResourceDictionary>
I want to have some default styles for a TextBlock which works
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="FontFamily" Value="Comic Sans MS"/>
<Setter Property="FontSize" Value="14"/>
</Style>
I found somewhere to use x:Key but I can't get it to work.
I will have several TextBlock's, How do I identify a TextBlock in the UI XAML as Red, Yellow or Green TextBlock and apply that style to them? This isn't recognized x:Key="GreenTextBlock"
<TextBlock x:Key="GreenTextBlock" Grid.Column="1" Margin="0,10,0,0" TextWrapping="Wrap" Text="PO Manager" VerticalAlignment="Top" TextAlignment="Center" FontWeight="ExtraBold"/>
If u want apply style on for example all TextBlocks in application, just use style without x:key defined e.g
<Style TargetType="{x:Type TextBlock}">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="FontFamily" Value="Comic Sans MS"/>
<Setter Property="FontSize" Value="14"/>
</Style>
When you are applying a style, use TargetType="{x:Type TextBlock}" instead of TargetType="TextBlock"
When do you use want to base on other style use
BasedOn="{StaticResource StyleUWantToBaseOn}"
where StyleUWantToBaseOn is style with x:Key property
And when you want to apply a specific style on lets say textblock you want to use Style property e.g:
<TextBlock Style="{StaticResource GreenTextBlock}" Grid.Column="1" />

WPF Modifying scrollbar in DataGrid issue

I am trying to style the scrollbars in my datagrids using app.xaml (want it to be global across the app)
My code I have done is
<Style TargetType="{x:Type DataGrid}">
<Setter Property="ScrollViewer.Background" Value="LightBlue"></Setter>
<Setter Property="ScrollViewer.Foreground" Value="LightGray"></Setter>
</Style>
However, it doesn't work, is there anything I have missed?
Cheers
Try this
<Style TargetType="{x:Type DataGrid}">
<Style.Resources>
<Style TargetType="ScrollViewer">
<Setter Property="Background" Value="LightBlue"/>
<Setter Property="TextElement.Foreground" Value="LightGray"/>
</Style>
</Style.Resources>
</Style>
Note : for scrollbar change target type <Style TargetType="Scrollbar">

Can I override/replace a property of a child-item in a default style

The default wpf MenuItem (on Menu) is constructed out of controls approx. like this:
grid; outer-rectangle; bg-rectangle; inner-rectangle; dockpanel; popup.
The dockpanel in turn consists of:
contentpresenter[icon]; path; contentpresenter[text]
The contentpresenter[text] consists of a TextBlock control.
What I want to achieve is to define a Style, as simple as possible, to change the VerticalAlignment property of this TextBlock, but only for the TextBlock in MenuItem, not in general.
<Style x:Key ="TextBlockCenterStyle" TargetType="{x:Type TextBlock}">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
<Style TargetType="MenuItem">
<Setter Property="FontSize" Value="11" />
<Setter Property="ItemContainerStyle" Value="TextBlockCenterStyle" />
<Style.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</Style.Resources>
</Style>
I tried Style.Resources and ItemContainerStyle.
Cannot get it to work. ItemContainerStyle throws TargetInvocationException (from NullReferenceException) at run-time.
When it is possible it should be a general solution, something like FindChildControl?!
Did you try ItemContainerStyle?
Something like:
<MenuItem ItemContainerStyle = {StaticResource MyItemContainerStyle}../>
Then the MyItemContainerStyle have your
<Style x:Key ="MyItemContainerStyle" TargetType="{x:Type TextBlock}">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
=============== answer after EDIT ======================
try this:
<Style TargetType="MenuItem">
<Setter Property="FontSize" Value="11" />
<Setter Property="ItemContainerStyle" Value="{StaticResource TextBlockCenterStyle}" />
<Style.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</Style.Resources>
</Style>

How to apply more than two style to one control in wpf? [duplicate]

This question already has answers here:
How to apply multiple styles in WPF
(11 answers)
Closed 8 years ago.
<Window.Resources>
<Style x:Key="Style_1" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Green"/>
</Style>
<Style x:Key="Style_2" TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="White"/>
</Style>
<Style x:Key="Style_3" TargetType="{x:Type Button}">
<Setter Property="BorderBrush" Value="Red"/>
</Style>
<Grid x:Name="LayoutRoot">
<Button Content="MultyStyles" Margin="160,136,248,222"/>
Now i want to apply Above Three Style into one control.I can apply two style to one Controle like below.
<Window.Resources>
<Style x:Key="Style_1" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Green"/>
</Style>
<Style x:Key="Style_2" TargetType="{x:Type Button}" BasedOn="{StaticResource Style_1}">
<Setter Property="Foreground" Value="White"/>
</Style>
<Style x:Key="Style_3" TargetType="{x:Type Button}">
<Setter Property="BorderBrush" Value="Red"/>
</Style>
<Grid x:Name="LayoutRoot">
<Button Content="MultyStyles" Margin="160,136,248,222" Style="{StaticResource Style_2}"/>
</Grid>
But i don't know to apply more than two style to one control. what should i do for that ?
You can only do this using the BasedOn logic you've already used. As an option, you can use Triggers to change the style if it makes sense in your project.

How to share wpf style setter

I have the following ResourceDictionary:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="StyleComboBox" TargetType="{x:Type ComboBox}">
<Setter Property="BorderBrush" Value="DarkGray" />
<Setter Property="BorderThickness" Value="1" />
<!-- Styles for ComboBox -->
</Style>
<Style x:Key="StyleTextBox" TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="DarkGray" />
<Setter Property="BorderThickness" Value="1" />
<!-- Styles for Textbox -->
</Style>
</ResourceDictionary>
How is it possible to use only at one position the setter?
Styles in wpf can be inherited from another style.
<Style x:Key="baseStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="12" />
<Setter Property="Background" Value="Orange" />
</Style>
<Style x:Key="boldStyle" BasedOn="{StaticResource baseStyle}" TargetType="TextBlock">
<Setter Property="FontWeight" Value="Bold" />
</Style>
source
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="baseStyle" TargetType="Control">
<Setter Property="BorderBrush" Value="DarkGray" />
<Setter Property="BorderThickness" Value="1" />
</Style>
<Style x:Key="StyleComboBox" BasedOn="{StaticResource baseStyle}" TargetType="{x:Type ComboBox}">
<!-- Styles for ComboBox -->
</Style>
<Style x:Key="StyleTextBox" BasedOn="{StaticResource baseStyle}" TargetType="{x:Type TextBox}">
<!-- Styles for Textbox -->
</Style>
</ResourceDictionary>
<Style TargetType="Control" x:Key="Controlbase">
<Setter Property="Control.BorderThickness" Value="10"/>
</Style>
<Style x:Key="StyleComboBox" TargetType="{x:Type ComboBox}" BasedOn="{StaticResource Controlbase}">
<Setter Property="BorderBrush" Value="DarkGray" />
<!-- Styles for ComboBox -->
</Style>
<Style x:Key="StyleTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource Controlbase}">
<Setter Property="BorderBrush" Value="DarkGray" />
<!-- Styles for Textbox -->
</Style>
I hope this will help.
Curious if this worked for you. Need to be careful in that you're redefining the style of say a ComboBox as based on your Control base style. Presumably the control template is not affected by this as that would be bad. IOW A ComboBox is much more than just a simple control and needs to inherit the styles and preserve the control template of all that it means to be a ComboBox. IE Its a SelectorControl that inherits from an ItemsControl etc.
I wonder if rebasing its style would also cause it to prefer/use the default control template of a Control rather than retaining its "identity" as say a ComboBox.

Resources