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.
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 have problem with FocusVisualStyle in my application.
I would like to disable it globally, but I cannot get it done. I have created 2 styles:
<Style TargetType="{x:Type Control}">
<Setter
Property="FocusVisualStyle"
Value="{x:Null}" />
</Style>
<Style TargetType="{x:Type FrameworkElement}">
<Setter
Property="FocusVisualStyle"
Value="{x:Null}" />
</Style>
And placed it at the beginning of ResourceDictionary.MergedDictionaries of my App.
Unfortunately, this is not working.
I am using MahApp.Metro and DevExpress and this might be a problem here.
Do you have any solution for that?
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 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?