i created a ResourceDictionary , and defined a style for Windows
<Style TargetType="{x:Type Window}" x:Key="WindowDefaultStyle">
<Setter Property="FontFamily" Value="Tahoma" />
<Setter Property="FlowDirection" Value="RightToLeft" />
<Setter Property="FontSize" Value="11" />
</Style>
<!-- Window file -->
<Window Style="{DynamicResource ResourceKey=WindowDefaultStyle}">
apply style in design but when run program not apply.:(
Note: I have updated my code so other people can simply use it.
Try setting the x:Keyon the Style along with TargetType like this -
<Style x:Key="{x:Type Window}" TargetType="{x:Type Window}">
<Setter Property="FontFamily" Value="Tahoma" />
<Setter Property="FlowDirection" Value="RightToLeft" />
<Setter Property="FontSize" Value="11" />
</Style>
Edit:
You need to explicitly apply style to your window by giving your style some key. For reference please see these links -
WPF window style not being applied
How to set default WPF Window Style in app.xaml?
Related
Is it possible to define a ResourceDictionary in a Style?
For example, suppose I wanted to have two different Styles for StackPanels and in one I want all the buttons to be blue and the other I want them to be red. Is this possible?
Something like
<Style x:Key="RedButtonsPanel" TargetType="{x:Type StackPanel}">
<Setter Property="Orientation" Value="Horizontal" />
<Setter Property="StackPanel.Resources">
<Setter.Value>
<ResourceDictionary>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Red" />
</Style>
</ResourceDictionary>
</Setter.Value>
</Setter>
</Style>
The above code fails with an error about the Property value of a Setter cannot be null (even though it's obviously not null).
I can do something like
<ResourceDictionary x:Key="RedButtons">
<Style TargetType="{x:Type Button}">
<Setter Property="Width" Value="100" />
<Setter Property="Background" Value="Red" />
</Style>
</ResourceDictionary>
<StackPanel Resources={StaticResource RedButtons} />
However I was wondering if there was a way to merge the ResourceDictionary into the style.
Try adding the Style(s) for each TargetType to the DockPanel Style.Resources.
I did something similar with a DockPanel Style. Wanted all Buttons or Separators added to the DockPanel to get styled in a consistent manner.
Here's a sample:
<Style x:Key="DockPanelToolBarStyle" TargetType="{x:Type DockPanel}">
<Style.Resources>
<Style TargetType="Button" BasedOn="{StaticResource ButtonToolBarStyle}" />
<Style TargetType="Separator" BasedOn="{StaticResource SeparatorToolBarStyle}" />
</Style.Resources>
<Setter Property="Height" Value="45"/>
<Setter Property="Background" Value="{StaticResource ToolBarBrush}"/>
</Style>
StackPanel.Resources is not a DependencyProperty and therefore I don't believe you will be able to set that property within the style.
I'm trying to figure out how to change the style of the AvalonEdit CodeCompletion window. However, I can't figure out the right combination of xaml style target/properties to change it. The main thing I'd like to do is get rid of the border, but maybe some additional changes as well.
Here is the xaml I've tried. None of it is affecting the UI.
xmlns:ae="clr-namespace:ICSharpCode.AvalonEdit.CodeCompletion;assembly=ICSharpCode.AvalonEdit"
<Style TargetType="{x:Type ae:CompletionWindow}">
<Setter Property="WindowStyle" Value="None" />
</Style>
<Style TargetType="{x:Type ae:CompletionWindowBase}">
<Setter Property="WindowStyle" Value="None" />
</Style>
<Style TargetType="{x:Type ae:CompletionListBox}">
<Setter Property="Background" Value="Red" />
</Style>
<Style TargetType="{x:Type ae:CompletionList}">
<Setter Property="Background" Value="Orange" />
</Style>
Use this style to remove border on window:
<Style TargetType="{x:Type avalonEdit:CompletionWindow}">
<Setter Property="WindowStyle" Value="None"></Setter>
<Setter Property="ResizeMode" Value="NoResize"></Setter>
<Setter Property="BorderThickness" Value="0"></Setter>
</Style>
To make the styles affect the UI, you can put them in a resource dictionary xaml and parse that with (ResourceDictionary)XamlReader.Parse(ResourcesAsXaml).
Then assign the ResourceDictionary to the Resources property of the CompletionWindow.
Under <Window.Resources>, I have the following style defined:
<Style TargetType="TextBox">
<Setter Property="Height" Value="22" />
<Setter Property="Width" Value="125" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="Background" Value="WhiteSmoke" />
</Style>
It works fine until I needed to inherit the style on another style
<Style BasedOn="{StaticResource TextBoxStyle}" TargetType="{x:Type PasswordBox}">
Which means I need to add the x:Key=TextBoxStyle to the Text box style above.
But when I do this, the styling for the text box breaks altogether.
I tried doing the same to Button styling, and the same thing happens, where the style will break if I add a key to it.
The only solution I thought of is to individually add the style to the elements, but that is what I am trying not to do.
No, you do not need to add x:Key to reference it:
<Style BasedOn="{StaticResource {x:Type TextBox}}" TargetType="{x:Type PasswordBox}">
Well to provide a better understandability and maintainance, I would prefer the following approach. IMHO, another programmer could get better into the code, if the implicities are reduced to a minimum.
<Style x:Key="BasicTextBoxStyle" TargetType="{x:Type TextBox}">
<!--some settings here-->
</Style>
<!--Declare BasicTextBoxStyle as default style for TextBoxes-->
<Style BasedOn="{StaticResource BasicTextBoxStyle}" TargetType="{x:Type TextBox}"/>
<!--Create some special style based on the basic style-->
<Style BasedOn="{StaticResource BasicTextBoxStyle}" TargetType="{x:Type PasswordBox}">
<!--some more specific settings-->
</Style>
Just my two cents...
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.
I need to remove the selected state (effect) or whatever it is called from every control in my interface. You know the black dashed line...
What are the ways for it to be done?
P.S. Is it normal for a fully customized XAML page to use 30MB RAM?
Thanks in advance.
That is controls by the FocusVisualStyle of the associated control. Unfortunately, you cannot disable that globally for all controls using a single Style or setting. Instead, you'd have to individually turn it off for every control type.
For example, you can include the following styles in your Application.Resources to turn it off for the specified controls:
<Style TargetType="Button">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
<Style TargetType="RepeatButton">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
<Style TargetType="ToggleButton">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
<Style TargetType="TreeViewItem">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
<!-- ETC -->
But keep in mind, that if you use the Style property on any of the controls or if you have any other implicit styles defined then those will prevent the styles above from being applied.
Or as Rachel point out, you could do this:
<Style x:Key="FrameworkElementStyleKey" TargetType="FrameworkElement">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
<Style TargetType="Button" BasedOn="{StaticResource FrameworkElementStyleKey}" />
<Style TargetType="RepeatButton" BasedOn="{StaticResource FrameworkElementStyleKey}" />
<Style TargetType="ToggleButton" BasedOn="{StaticResource FrameworkElementStyleKey}" />
<Style TargetType="TreeViewItem" BasedOn="{StaticResource FrameworkElementStyleKey}" />
<!-- ETC -->
Functionally, both approaches above have the same effect.