WPF style like css classes - wpf

I have a tooltip style like below:
<Style TargetType="{x:Type ToolTip}">
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="HasDropShadow" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToolTip">
...
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I have two button with each with the defined tooltip but I want only one of them has my tooltip style.
How can I achieve this?

Add the following to your Tooltip style:
<Style TargetType="{x:Type ToolTip}" x:Key="myTooltipStyle">
Now, only the button which will specifically ask for the myTooltipStyle will get it:
<Button ToolTip="{StaticResource myTooltipStyle}">

Related

How to set style and template for DataGridRow on custom DataGrid

I have a custom DataGrid element. Stle and template of this element described in Theme.xml:
<Style TargetType="{x:Type local:MyDataGrid}" BasedOn="{StaticResource {x:Type DataGrid}}">
<Setter Property="RowHeaderWidth" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGrid}">
<!-- ... -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
How do i specify template for DataGridRow specially for my custom DataGrid.
Something like this:
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridRow}">
<Border BorderThickness="3">
<DataGridCellsPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
litterary changing nothing.
Setting x:Key value for DataGridRow style and then usining it in my custom datagrid style with Setter Property changing nothing too.
You could set the RowStyle property, either in the Style:
<Style TargetType="{x:Type local:MyDataGrid}" BasedOn="{StaticResource {x:Type DataGrid}}">
<Setter Property="RowHeaderWidth" Value="0"/>
<Setter Property="Template">
...
</Setter>
<Setter Property="RowStyle">
<Setter.Value>
<Style TargetType="{x:Type DataGridRow}">
...
</Style>
</Setter.Value>
</Setter>
</Style>
...or on the MyDataGrid element:
<local:MyDataGrid>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
...
</Style>
</DataGrid.RowStyle>
</local:MyDataGrid>

How to override Foreground for selected ListBoxItem

I have a global style (Application.Resources) to set the Foreground of all TextBlocks.
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Brown"/>
</Style>
This works fine.
Now I try to override the Foreground of the TextBlock inside of a selected ListBoxItem, which is part of default ContentPresenter content.
I created a new global style for the ListBoxItem:
<Style TargetType="ListBoxItem">
<Setter Property="Foreground" Value="Orange" />
<Setter Property="Background" Value="Aqua"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Background="{TemplateBinding Background}">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Brown" />
<Setter Property="Foreground" Value="Aqua" />
</Trigger>
</Style.Triggers>
</Style>
The background works fine.
But Foreground has still the brush form de global style from the TextBlock.
Which is the best way to set the Foreground in a solution that works with Binding?
This is an example of why defining an implicit application-wide TextBlock style is usually a bad idea.
But you should be able to override it by adding a default style to to <ContentPresenter.Resources>:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Background="{TemplateBinding Background}">
<ContentPresenter>
<ContentPresenter.Resources>
<Style TargetType="{x:Type TextBlock}" BasedOn="{x:Null}" />
</ContentPresenter.Resources>
</ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>

Change a Border Background inside a BasedOn ControlTemplate

Here is the XAML in my application resources that globally changes all of the Button controls in the application to look and behave like I want:
<Style TargetType="{x:Type Button}" x:Key="MyButtonStyle">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="Border" CornerRadius="0" BorderThickness="0"
Background="CornflowerBlue" BorderBrush="CornflowerBlue">
<ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True" />
</Border>
<ControlTemplate.Triggers>
<!-- a bunch o' triggers here -->
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
On one of my application's user controls, I would like to change some properties of this button. Here is some XAML that I am using in the UserControl.Resources section now to do this:
<Style x:Key="SpecialButton" TargetType="Button" BasedOn="{StaticResource MyButtonStyle}">
<Setter Property="Width" Value="20" />
<Setter Property="Visibility" Value="Collapsed" />
<Setter Property="Content" Value=">" />
<Setter Property="Border" Value="#eeeeee" />
<Setter Property="Border.Background" Value="#eeeeee" />
</Style>
The Button controls on my UserControl that I assign the style to of SpecialButton have the correct width, visibility, and content, but these last two attempts do not work. How would I go about changing the background color of the Border with a name of "Border" from the application resource in this SpecialButton style?
What you can do is use TemplateBinding to set the Background property on the control, in the base style. Also in the base style, set the Background color to the default "CornflowerBlue".
<Setter Property="Background" Value="CornflowerBlue" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="Border" Background="{TemplateBinding Background}"
Now you can overwrite the Background in the derived style:
<Style x:Key="SpecialButton" TargetType="Button" BasedOn="{StaticResource MyButtonStyle}">
<Setter Property="Background" Value="#eeeeee" />
(Note that, if you want to use other properties that aren't defined on the Button control -- or, say you wanted to use multiple background colors -- then you'd have to create your own control that inherits Button, and expose the new properties as Dependency Properties.)

wpf ToolTip and Style

How I can add ToolTip to this code ?
<Style TargetType="{x:Type ListBoxItem}">
</Style>
You can set ToolTip property in a style the same way you set any other property.
Sample code as below
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="ToolTip" Value="ToolTip Value" />
</Style>
For a more complex ToolTip use
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="ToolTip">
<Setter.Value>
<!--Content Here-->
<!--<Grid> or <StackPanel> or <ContentPresenter>...-->
</Setter.Value>
</Setter>
</Style>

Style vs. ControlTemplate

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>

Resources