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>
Related
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>
I want to define the design of my TextBox from static resource, how to apply that?
For now I have:
<TextBox Style="{StaticResource TextBoxHeight }" />
And here Page.Resources:
<Page.Resources>
<Style x:Key="TextBoxHeight" TargetType="{x:Type TextBox}" >
<Setter Property="Height" Value="20"/>
</Style>
<Style x:Key="TextBoxBorder" TargetType="{x:Type Border}" >
<Setter Property="CornerRadius" Value="10"/>
</Style>
</Page.Resources>
But I need that:
<TextBox Style="{StaticResource TextBoxHeight }" Style="{StaticResource TextBoxBorder }" />
But it gives error "The property 'Style' is set multiple times"
You can't set the Style property more than once. And you can't apply a Style with a TargetType of Border to a TextBox. But putting an implicit Border style in the Resources dictionary of the Button style should work:
<Style x:Key="TextBoxHeight1" TargetType="{x:Type TextBox}" >
<Setter Property="Height" Value="20"/>
<Style.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="CornerRadius" Value="10"/>
</Style>
</Style.Resources>
</Style>
I have a UI in Silverlight with some borders, and with a TextBlock inside that borders. There will be other TextBlocks outside borders.
<Border>
<TextBlock>This text should be centered</TextBlock>
</Border>
<Border>
<TextBlock>This one too</TextBlock>
</Border>
<TextBlock>this one shouldn't</TextBlock>
What I'm trying to achieve is to format all TextBlock's inside a Border, without having to set the style in every TextBlock. If it was CSS , it will be something like that : .border .textBlock { (...) }
I know i can set a style inside the scope of a border:
<Border>
<Border.Resources>
<Style TargetType="TextBlock" BasedOn="{StaticResource DefaultTextBlockStyle}">
<Setter Property="HorizontalAlignment" Value="Center"></Setter>
</Style>
</Border.Resources>
<TextBlock>Centered Text</TextBlock>
</Border>
But I would still need to set this to every border in my page. So now i presente the question, can i set in a style, in order to set it one time to affect all Borders? I tried the code bellow, but it didn't work. It didn't give me any errors, but it didn't affect the TextBlock formatting either.
<Style TargetType="Border">
<Setter Property="Resources">
<Setter.Value>
<ResourceDictionary>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</ResourceDictionary>
</Setter.Value>
</Setter>
</Style>
Try this:
<Style TargetType="Border">
<Style.Resources>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</Style.Resources>
</Style>
This works in WPF. In Silverlight I am afraid you cannot do this though.
I have two styles, which are almost identical:
<Style x:Key="CancelButtonStyle" TargetType="{x:Type Button}" >
<!-- gigantic common code -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<!-- more common code -->
<Path Data="DIFFERENT_VALUE_A"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="OkButtonStyle" TargetType="{x:Type Button}" >
<!-- gigantic common code -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<!-- more common code -->
<Path Data="DIFFERENT_VALUE_B"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I want to extract the common code ("style template"), and then use it like that:
<Style x:Key="OkButtonStyle" Base="PathButtonStyle" PathData="DIFFERENT_VALUE_B" />
EDIT:
As I specified in title, I don't want to create my own control
Just define your ControlTemplate objects separately in Resources:
<ControlTemplate x:Key="ControlTemplate1">
<!-- different part -->
</ControlTemplate>
<ControlTemplate x:Key="ControlTemplate2">
<!-- different part -->
</ControlTemplate>
Create one Style for the common parts:
<Style x:Key="BaseStyle" TargetType="{x:Type Button}" >
<!-- gigantic common code -->
</Style>
Then base your new styles on that one, referencing your new ControlTemplates:
<Style x:Key="CancelButtonStyle" TargetType="{x:Type Button}"
BasedOn="{StaticResource BaseStyle}">
<Setter Property="Template" Value="{StaticResource ControlTemplate1}" />
</Style>
<Style x:Key="OkButtonStyle" TargetType="{x:Type Button}"
BasedOn="{StaticResource BaseStyle}">
<Setter Property="Template" Value="{StaticResource ControlTemplate2}" />
</Style>
You could use Tag to pass the PathData:
<Style x:Key="BaseStyle" TargetType="{x:Type Button}" >
<!-- gigantic common code -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<!-- more common code -->
<Path Data="{TemplateBinding Tag}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and then set the Tag in your individual Style:
<Style x:Key="CancelButtonStyle" TargetType="{x:Type Button}"
BasedOn="{StaticResource BaseStyle}">
<Setter Property="Tag" Value="MY PAth A" />
</Style>
<Style x:Key="OkButtonStyle" TargetType="{x:Type Button}"
BasedOn="{StaticResource BaseStyle}">
<Setter Property="Tag" Value="MY PAth B" />
</Style>
You can create your own Button, which derives from the Wpf-Button and has the property PathData:
public class PathButton:Button
{
public string PathData {get;set;}
}
The your style could be like:
<Style x:Key="PathButtonStyle" TargetType="{x:Type ns:PathButton}" >
<!-- gigantic common code -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<!-- more common code -->
<Path Data="{TemplateBinding PathData}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
To use it you have to do this:
<ns:PathButton Style="{StaticResource PathButtonStyle}" PathData="M200 200 100 100 L0 0"/>
Or you create your own Custom Control, the way would be the same.
The adventage here is no matter how many different buttons you want to use, there always will be just one Style in your resources and one class (PathButton).
You just have to change the PathData-property of your PathButton to change it's appereance.
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}">