It was my understanding that this:
<Style TargetType="{x:Type Image}">
<Setter Property="Margin" Value="0"/>
<Setter Property="Stretch" Value="None"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Cursor" Value="Help"/>
</Style>
would change the default image settings for each image rendered on the Window. Is this not the case? Is there an exception for images as it appears to work with other controls?
TIA
To set a style for a type you actually need to set the x:Key attribute like so:
<Style x:Key="{x:Type Image}" TargetType="{x:Type Image}">
<!-- setters here -->
</Style>
Related
One of my Grid currently starts with the following code:
<Grid x:Name="Top_GRID" Margin="4.953,10" Width="817.28">
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
<Setter Property="Background" Value="Red" />
</Style>
<Style TargetType="TextBox">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
</Style>
<Style TargetType="Button">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
</Style>
</Grid.Resources>
Just to clarify - I want to declare a Grid in which all TextBlocks are having the Background property set to "Red". All Button margins are set to "3" and so on.
I'd like to move the resource definition to a dictionary file.
Should I somehow wrap it as a Style?
If so, I'm going to have a recursive style declaration which (I think is illegal).
Sounds simple but I can't find the way to do it.
Try this
<Style x:Key="Grid_ControlStyle" TargetType="Grid">
<Style.Resources>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
<Setter Property="Background" Value="Red" />
</Style>
<Style TargetType="TextBox">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
</Style>
<Style TargetType="Button">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
</Style>
</Style.Resources>
</Style>
You Need to put all styles for button, TextBox etc. in resourceDictionary file. Then add this file in application resources:
<Application x:Class="Aplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources\YourResource.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Then in xaml you will be able to use it like this
<TextBlock Grid.Column="1" Grid.Row="1" Text="bla bla" Style="{DynamicResource YourStyle}"/>
Finally your style should look like
<Style x:Key="StyleName" TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="3,3,3,3"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontSize" Value="12pt"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
Hope thats what you was looking for.
Please go through the concept of resource dictionary in WPF. Any style, colors, fonts etc whatever is related to look of the application which you wish to be repeated in more than one screen of your application shpuld be placed in the resource dictionary.
x:Key is the property which can be used to access the style anywhere across the application.
For your resource dictionary to be accessible througout the application, place in it app.xaml
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 can I have a WPF style that has no target type ( one that can be applied to all objects) ?
<Style x:Key="Basic" TargetType="???">
<Setter Property="FontFamily" Value="Tahoma"/>
<Setter Property="FontSize" Value="12"/>
</Style>
I want to base all other styles on this "basic" style.
Regards,
MadSeb
Append "Control." to the beginning of the Property, and remove the TargetType. Then, in the styles that derive from it, use BasedOn with a StaticResource pointing at the base Style.
<Style x:Key="basicStyle">
<Setter Property="Control.FontFamily" Value="Tahoma" />
<Setter Property="Control.FontSize" Value="12" />
</Style>
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource basicStyle}">
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource basicStyle}">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource basicStyle}">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Margin" Value="2,4" />
</Style>
In Silverlight 4 it's possible to use implicit styling - and that is amazing! But what if I want to apply a style to all of my Buttons, CheckBoxes and RadioButtons (all inheriting from ButtonBase)? I can't set TargetType on the Style to ButtonBase - that doesn't work. Do I need to create a style to each of the 3 control types?
http://www.silverlightshow.net/items/Implicit-Styles-in-Silverlight-4.aspx
Try this
xamlgeek,
The following implicit styles work well for me. I first create some name/keyed styles, using common BasedOn styles whereever possible. Then I simply create the implicit styles BasedOn those named/keyed styles...
<Style x:Key="BaseStyle" TargetType="Control">
<Setter Property="FontFamily" Value="{StaticResource FontFamily}" />
<Setter Property="FontSize" Value="{StaticResource FontSize}" />
<Setter Property="Foreground" Value="{StaticResource FontBrush}" />
</Style>
<Style x:Key="BaseStyleCentered" TargetType="Control" BasedOn="{StaticResource BaseStyle}">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
<Style x:Key="CommonCheckBox" TargetType="CheckBox" BasedOn="{StaticResource BaseStyleCentered}">
<Setter Property="Cursor" Value="Hand" />
</Style>
<Style x:Key="CommonRadioButton" TargetType="RadioButton" BasedOn="{StaticResource BaseStyleCentered}">
<Setter Property="Cursor" Value="Hand" />
</Style>
<Style x:Key="CommonButton" TargetType="Button" BasedOn="{StaticResource BaseStyleCentered}">
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Padding" Value="10,0,10,0" />
<Setter Property="MinWidth" Value="{StaticResource ButtonWidth}" />
<Setter Property="MinHeight" Value="{StaticResource ButtonHeight}" />
</Style>
<Style TargetType="CheckBox" BasedOn="{StaticResource CommonCheckBox}">
</Style>
<Style TargetType="RadioButton" BasedOn="{StaticResource CommonRadioButton}">
</Style>
<Style TargetType="Button" BasedOn="{StaticResource CommonButton}">
</Style>
Good luck,
Jim
YinYangMe, YinYangMoney and FaceToFaceSoftware
I am trying to work out a style for a ComboBox that has a navy background with white text, so I want the drop down arrow to be white also (the xaml I have so far is below).
<Style x:Key="ComboBoxStyle" TargetType="ComboBox">
<Setter Property="Background" Value="{StaticResource headerBrush}"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="BorderBrush" Value="{StaticResource headerBorderBrush}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="MinWidth" Value="100"/>
<Setter Property="Height" Value="21"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Padding" Value="5"/>
<Setter Property="Margin" Value="3"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
<Style x:Key="ComboBoxItemStyle" TargetType="ComboBoxItem">
<Setter Property="Background" Value="AliceBlue"/>
<Setter Property="Foreground" Value="Navy"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
</Style>
ADDED code to set the ControlTemplate?
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Path x:Name="Arrow" Fill="White"/>
</ControlTemplate>
</Setter.Value>
</Setter>
You need to edit the ControlTemplate of ComboBox and you can see a the Arrow as a Path. So change the Fill property of the Path to your desired arrow color. See sample ControlTemplate here
http://msdn.microsoft.com/en-us/library/ms752094.aspx