Guys im trying to using a style from Style.xaml into my code behind
on my style i have a code like this
file Style.xaml
<SolidColorBrush x:Key="FontGrey" Color="#FFC5C0C0"></SolidColorBrush>
and on my Apptest.xaml.cs file i have code like this
txt.Foreground = new SolidColorBrush(Color.FromArgb(255, 252, 147, 25));
if i want to change my foreground color base on style.xaml
how can i do that? i was trying using resources but it doesnt work
note: Style.xaml and Apptest.xaml are separated
You can access your defined resources, in Silverlight, by using the following syntax:
txt.Foreground = (SolidColorBrush)Application.Current.Resources["FontGrey"];
You can put your style into Window.Resources in Apptest.xaml like this:
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="Style1.xaml">
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Then in window code behind file Apptest.xaml.cs you can access to resource:
InitializeComponent();
txt.Foreground = Resources["FontGrey"] as SolidColorBrush;
If assume that resources is avaliable, than this code should work for you:
txt.Foreground = (Brush)FindResource("FontGrey");
Related
I need to change default white background that is showing on the right side when I am resizing the window of UWP application, and I need to do it dynamically.
I have tried:
var newBackground = Application.Current.Resources["ApplicationPageBackgroundThemeBrush"] as SolidColorBrush;
if (newBackground != null)
{
newBackground.Color = newColor;
}
The newBackground is changed, but not affecting the application.
Any help?
If you want to override ApplicationPageBackgroundThemeBrush
Application.Current.Resources["ApplicationPageBackgroundThemeBrush"] = Colors.Red;
For your issue we need to check something in background:
When you check the ApplicationPageBackgroundThemeBrush in the generaic.xaml(To konw what is generaic.xaml you can see here), you will find that ApplicationPageBackgroundThemeBrush is defined three times in "Default","HighContrast" and also "Light". So that when you call request theme all colors will be changed in different themes.
this.RequestedTheme = ElementTheme.Dark
So go back to your question, if you change the request theme to "Dark" you will find that the change color code:
newBackground.Color = newColor;
will not change since there is a default setting for "Dark".(It works for default/Light theme)
And it seems we cannot modify this theme brush at runtime from code behind.
I think the only way for this is to create theme color yourself then change the color by set the element explictly.
To set the theme color. New acrylic document provide a good point for us. Here I write a simple sample for you to show how default theme works:
Create a dictionary and insert the following code:
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<SolidColorBrush x:Key="ApplicationPageBackgroundThemeBrush" Color="Blue"></SolidColorBrush>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<SolidColorBrush x:Key="ApplicationPageBackgroundThemeBrush" Color="Yellow"></SolidColorBrush>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
Add it to app.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
To explictly set the element, do something like mygrid.background=xxxx. If you want to trigger it when resize, change the propery in adaptivetrigger.
In code behind (filename.xaml.cs file), I can successfully access the static resources like this:
TextBlock elm = new TextBlock();
elm.Style = (Style)this.Resources["myStyle"];
where Styles.xaml is added to filename.xaml like follows:
<Page.Resources>
<ResourceDictionary Source="resources/Styles.xaml" />
</Page.Resources>
However, this.Resources["myStyle"] doesn't work in .cs file that isn't associated with any .xaml file. How to access Style.xaml in this case?
You should use FindResource.
Either using this as a FrameworkElement:
elm.Style = (Style)this.FindResource("myStyle");
Or on the Application:
elm.Style = (Style)Application.Current.FindResource("myStyle");
I am trying to implement Style Binding from this article in WPF & Silverlight.
I have a resource dictionary, generic.Xaml with this code:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/AComponent;component/Themes/MyCustomStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
Where MyCustomStyles.xaml begins like this
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<t:ThemeColorProvider x:Key="ThemeProvider"/>
I need to get the instance of ThemeProvider to update colors/brushes that I am binding to in Generic.xaml. Is it possible to get the instance of the resource keyed "ThemeProvider" so I can update it?
Extra credit if you know a cross platform WPF & Silverlight implementation!
Note: I need to get this outside of the assembly that declares Generic.xaml
If your resource is defined in generic.xaml or any resource that is defined in MergedDictionaries in App.xaml then you need to use Application.Current.Resources, e.g.:
BackgroundColor =
(Color)Application.Current.Resources["ApplicationBarBackgroundColor"]
This may help:
ThemeColorProvider value= (ThemeColorProvider)FindResource("ThemeProvider");
// update value
I have a style file for Styles in WPF XAML with name Brushes.xaml which stores all colors for the WPF.
Code Here:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="DefaultColor">SteelBlue</Color>
<Color x:Key="LightDefaultColor">LightSteelBlue</Color>
</ResourceDictionary>
I want to change the value of DefaultColor using C# code.
Use the DynamicResource extension instead of the StaticResource extension on all references to keys that can change at runtime.
Then you can use code like the following to change the value.
Application.Current.Resources["Default Color"] = System.Windows.Media.Colors.Red;
This can be done per object to...
public MyWindow()
{
InitializeComponent();
this.Resources["Default Color"] = System.Windows.Media.Colors.Red;
PART_DynamicButton.Resources["Default Color"] = System.Windows.Media.Colors.Red;
}
This is higher performance than clearing your entire merged resource dictionary and adding a new one if you only need to modify a few values.
Just remember that DynamicResource extension only works on DependencyProperties and Freezable objects instantiated in Xaml are usually frozen which prevents modifing their DependencyProperties. So don't try to change the color of a SolidColorBrush if the brush was instaniated in xaml.
Here is a workaround
<! -- Xaml -->
<SolidColorBrush x:Key="App_Page_Background" Color="White"/>
<Page Background="{DynamicResource App_Page_Background}"/>
// C# code
Application.Current.Resources["App_Page_Background"] = new SolidColorBrush(Colors.Red);
Rather than changing the XAML contents you should create one XAML file for each theme.
Then you can change the theme at runtime like this:
ResourceDictionary skin = new ResourceDictionary();
skin.Source = new Uri(#"" + themeName + ".xaml", UriKind.Relative);
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(skin);
I would like to know exactly how to dynamically use a Dictionary Resource in the C# code behind - ie.. I would like to load images at runtime from an image resource within a dictionary
I have a program that has 3 images in a WPF Dictionary - these are images set as image resources.
Then in the code behind of my WPF Window I want to load any one of the three images based on user initiated events.
There is no real code I have to show as nothing that I have done works.
Ideas?
First, make sure you've defined your image resources like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ImageSource x:Key="image1">images/image1.jpg</ImageSource>
<ImageSource x:Key="image2">images/image2.jpg</ImageSource>
</ResourceDictionary>
Secondly, I'm assuming that your WPF dictionary is in its own file. Now you have to make sure you've merged your dictionary into your main window's XAML (skip this step if your resource dictionary is defined inside of the window's XAML). In your window's XAML file, make sure you have something like this:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="myDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
Now, in your code-behind, you can use the FindResource() method to locate your image resource by it's key name (the value of the x:Key attribute on the ImageSource in the resource dictionary) like so:
imageControl.Source = (ImageSource)FindResource("image1");
Hope this helps!
This is an addition to the accepted answer:
When working within a ViewModel from MVVM, make sure to use the FindResource from the view where the resource directory is added.
<Window x:Class="My.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ViewModels="clr-namespace:My.ViewModels"
Title="USA Hockey Player Evaluation tool"
Icon="/USAHockeyPlayerEval;component/View/Images/HET.ico"
SizeToContent="WidthAndHeight"
MinHeight="500px" MinWidth="800px">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Images.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Window.DataContext>
<ViewModels:MainWindowMV/>
</Window.DataContext>
<StackPanel>
<Menu>
<MenuItem Header="File">
<MenuItem Header="Save"></MenuItem>
My view in this case is a window (I know not correct MVVM ;-) )
Image img = new Image();
img.Source = (ImageSource)WindowReference.FindResource("Pluse");
Here the WindowReference is a reference to My.MainWindow.