xaml Application Resource Value - wpf

I want to set an application wide Value i.e. TextHeight (others as well) and I can't seem to find a reference. IOW, set the Text Height to a StaticResource in various styles, etc.

My brain hurts a little bit after reading that question. Let me answer as if I truly understand what you're asking.
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<Style TargetType="TextBox">
<Setter Property="FontSize" Value="100"/>
</Style>
</Application.Resources>
</Application>
With clarification:
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
StartupUri="Window1.xaml">
<Application.Resources>
<sys:Double x:Key="MyTextHeight">32</sys:Double>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="{StaticResource MyTextHeight}"/>
</Style>
</Application.Resources>
</Application>
notice line 4, then the new Double (also note that the type must match the type of the parameter--I originally tried sys:Int32 which resulted in some interesting unrelated exceptions).

Related

Style Defined in app.xaml is only applied in the Designer but not a Runtime

I put the two following Style in App.xaml of my WPF application. If I change the FontSize to a different value, the Designer of Visual Studio 2019 shows all the controls with the specified FontSize. If I run the app, the controls show a FontSize of 12.
<Application x:Class="testapp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:testapp"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="View/Themes/ButtonStyle.xaml"/>
<ResourceDictionary Source="View/Themes/CheckBoxStyle.xaml"/>
<ResourceDictionary Source="View/Themes/ComboBoxStyle.xaml"/>
<ResourceDictionary Source="View/Themes/DataGridStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Page}">
<Setter Property="FontSize" Value="18" />
</Style>
<Style TargetType="{x:Type Window}">
<Setter Property="FontSize" Value="18" />
</Style>
</ResourceDictionary>
</Application.Resources>
If you research a bit, there has been an issue on this going back ten+ years on windows/pages. In design time the designer will get the style, but due to the Page/Window being derived types, during runtime they won't.
The fix (or workaround depending one one's viewpoint) is to name the style in app.xaml with x:key such as (page only shown for brevity):
<Style x:Key="pStyle" TargetType="{x:Type Page}">
<Setter Property="FontSize" Value="18" />
</Style>
and then set to the static resource style on each page/window such as below
Style="{StaticResource pStyle}"
such as:
<Page x:Class="WPFStack.ListBox.ListViewQuestion"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WPFStack.ListBox"
mc:Ignorable="d"
xmlns:model="clr-namespace:WPFStack.Model"
d:DesignHeight="450" d:DesignWidth="800"
Style="{StaticResource pStyle}"
Title="ListViewQuestion">
See How to set default WPF Window Style in app.xaml

WPF window's background color is not set by style automatically

I have created simple WPF application with one window. What I want is to apply background color automatically to all windows. However, the color isn't applied.
Here's link to sample project. The following is XAML in App:
<Application x:Class="SampleWPFApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SampleWPFApp"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="Window">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="#FF3B444B" />
</Setter.Value>
</Setter>
</Style>
<Style TargetType="local:MainWindow" BasedOn="{StaticResource Window}" />
</Application.Resources>
</Application>
The logic was taken from here.
EDIT:
Well, I wasn't a bit fare about when the background isn't set - it's not set in VS editor. When program runs, background is OK. The solutions of Ragavan and mm8 do work, albeit they are the same with the difference that BasedOn="{StaticResource Window}" lets us omit the style's key (being Window the key itself).
Alas, the editor doesn't show the background, although setting the style explicitly (in MainWindow's XAML) makes background appear.
Basedon Will not bind directly window . Replace this code BasedOn="{StaticResource {x:Type Window}}"
App.Xaml
<Application x:Class="SampleWPFApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SampleWPFApp"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="Window">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="#FF3B444B" />
</Setter.Value>
</Setter>
</Style>
<Style TargetType="local:MainWindow" BasedOn="{StaticResource {x:Type Window}}"/>
</Application.Resources>
</Application>
In WPF, you set the styling resource for a type and not for an instance of type. Hence, below line should be removed.
Style TargetType="local:MainWindow" BasedOn="{StaticResource Window}" />
In your case, Windows background is being applied but it is just not visible because it may have a panel within. For example, when you create a new window, it will have Grid by default. Add below line and you will get the background for entire window. Please append &LT character in the below lines.
&LTStyle TargetType="{x:Type Grid}" BasedOn="{x:Null}">
&LTSetter Property="Background" Value="Black" />
&LT/Style>
Just give your style an x:Key and base the MainWindow style on this one. This works wonders for me:
<Application x:Class="SampleWPFApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SampleWPFApp"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style x:Key="myWindowStyle" TargetType="Window">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="#FF3B444B" />
</Setter.Value>
</Setter>
</Style>
<Style TargetType="local:MainWindow" BasedOn="{StaticResource myWindowStyle}" />
</Application.Resources>
</Application>

Applying global styles to combobox

I'm trying to apply a global style to all ComboBoxes in my application. I'm doing this by defining a Style in my App.xaml file and specifying a TargetType, which should apply that style to all controls that are of the specified type. However, it appears that my style is not being applied at all.
Here's my code:
App.xaml
<Application x:Class="Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Test"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="Background" Value="Red"></Setter>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
MainWindow.xaml
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Test"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox Margin="173,130,186,166"></ComboBox>
</Grid>
</Window>
I do not have any code-behind at this point other than the default code that VS generates for WPF forms.
I expect this XAML code to change the background of any ComboBoxes in any window to red, without me needing to manually specify the style for each ComboBox. (I really don't want write it out manually for each ComboBox - my app will end up using many, many CBs and it would be a major pain - not to mention it looks cluttered.)
I tried to model my code after this question but did not get any results.
Try to avoid the nested ResourceDictionary in the App.Xaml.
Fix in this way:
<Application.Resources>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="Background" Value="Red"></Setter>
</Style>
</Application.Resources>
I would suggest to create a folder in your solution and add in it a Xaml control : ResourceDictionary where you gonna define all your global styles you want to apply by default.
For example :
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type TextBox}">
<Setter Property="Height" Value="25"/>
<Setter Property="Background" Value="red"></Setter>
</Style>
</ResourceDictionary>
Now, you just need to put a reference in you App.Xaml like this :
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Views/Style/GlobalStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Hope it'll help you.
Have a good day.

Trying to change window background color from XAML file

I'm playing around with the ExpressionDark.xaml theme. I'm setting the theme in App.xaml:
<Application x:Class="WpfApplication4.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary Source="Themes/ExpressionDark.xaml"/>
</Application.Resources>
</Application>
I looked at how they were setting the colors and styles of other controls, but am unable to produce the same result with the window.
If you like, you can see the XAML here.
Here's the XAML I'm trying:
<Style TargetType="{x:Type Window}">
<Setter Property="Background" Value="{DynamicResource BlackTestBrush}" />
<Style.Triggers>
</Style.Triggers>
</Style>
<SolidColorBrush x:Key="BlackTestBrush" Color="#FF000000" />
Any ideas on what I'm doing wrong here?
Thanks

Styles defined in Application.Resources not applying to controls

I am trying to apply global application styles to certain control types, however adding these styles to Application.Resources does is not applying the styles to the elements in my views.
Example:
<Application x:Class="GUI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="AliceBlue"></Setter>
<Setter Property="Margin" Value="20,20,20,20"></Setter>
<Setter Property="FontStyle" Value="Italic"></Setter>
</Style>
</Application.Resources>
</Application>
In all the examples I have found for applying application wide styles this has been how they say to do it, however it is not working for me. What am I doing wrong?
Thanks,
Alex.
Worked this out myself woops, the problem is I was not using the StartUpUri property to open my initial application view, I changed my start up process so it does use this property and this has fixed my problem.
My App.xaml now looks like this:
<Application x:Class="GUI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="/Views/Application/SplashView.xaml">
<Application.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Aqua"></Setter>
</Style>
</Application.Resources>
</Application>
Thanks,
Alex.
Eventhough this is an old post and has been answered. I came across this problem. I removed StartupUri and added and empty style (I used the question as an example):
<Application x:Class="GUI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<!-- Added blank style first -->
<Style TargetType="Rectangle" />
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="AliceBlue"></Setter>
<Setter Property="Margin" Value="20,20,20,20"></Setter>
<Setter Property="FontStyle" Value="Italic"></Setter>
</Style>
</Application.Resources>
</Application>

Resources