WPF - language changes only in MainWindow? - c#

I've programmed a choose-language option in my project, using MergedDictionaries and project's settings. The issue is the language changes successfully only in my MainWindow, and not in other Windows as well. What am I doing wrong?
Set-language func in MainWindow (edit: MainWindow.cs):
/*set language*/
private void SetLanguageDictionary()
{
ResourceDictionary dict = new ResourceDictionary();
if (Properties.Settings.Default.Language.Equals("en")) //english was set
{
dict.Source = new Uri("\\res\\enDictionary.xaml", UriKind.Relative);
}
else //otherwise - hebrew as default lang.
{
dict.Source = new Uri("\\res\\hebDictionary.xaml", UriKind.Relative);
}
//add required dictionary to the MergedDictionaries
Resources.MergedDictionaries.Add(dict);
}
A small example of one of the Dictionaries [they are set symmetrically, if it matters]:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:UI_WPF"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="employees">Employees</system:String>
<system:String x:Key="employers">Employers</system:String>
<system:String x:Key="contracts">Contracts</system:String> </ResourceDictionary>

You haven't told us where the SetLanguageDictionary() method is defined but if you want to apply the resources globally you could merge the ResourceDictionary into the global Application.Current.Resources:
Application.Current.Resources.MergedDictionaries.Add(dict);

you know why language changes in MainWindow only? because when you call SetLanguageDictionary()only MainWindow will refresh(reload), and that why labels and texts will change. In order to change language in other window, you need to refresh them - reload them again- and during reloading process, content and labels will be updated.
you can call other window from MainWindow like below
window win = new window();
//then
win.AnyMethodyou_want();
new window() will reload window again, then language can be changed.
I have used this way before..

Related

Accessing a DynamicResource in code-behind [duplicate]

I have a DataTemplate defined in a xaml file that I want to access via C# code.
Can anyone please tell me how can I access it?
I added a new ResourceDictionary file and its name is Dictionary1.xaml.
I have a data template such as:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="mytemplate">
<TextBlock Text="Name:" Background="Blue"/>
</DataTemplate>
</ResourceDictionary>
not I have a ListBox called listBox1 and I want to assign it to it's Itemtemplate property
but I'm not getting how can i do it?
Since Application.Current was null in my case, I've ended up using this:
var myResourceDictionary = new ResourceDictionary();
myResourceDictionary.Source =
new Uri("/DllName;component/Resources/MyResourceDictionary.xaml",
UriKind.RelativeOrAbsolute);
and then getting the specified key I needed by using
myResourceDictionary["KeyName"] as TypeOfItem
(source)
Where exactly are you defining it?
If you define it in the ResourceDictionary of your object, then
Application.Current.Resources[typeof(yourDataTemplateTargetType)]
should work. If you are defining it as a member of something else, like say, an ItemsControl, you need to get a handle to the ItemsControl instance and call the ItemTemplate property.
Edit: Ok, I think we're getting somewhere. So you are defining a ResourceDictionary in its own file. Before you can use it in your UI and access it from your code behind, you need to merge that ResourceDictionary into your application. Are you doing this?
If you are, then the next step is to get this resource. Each FrameworkElement has a method called FindResource. This method is great because it walks up the ResourceDictionary tree and attempts to locate the resource with the key. So, if you want to access this resource from a UserControl, you can do the following in the code behind:
FindResource(typeof(yourDataTemplateTargetType));
If this doesn't work for you, please show us exactly how you are declaring this resource dictionary and how it is getting merged into your application's resources.
If you for example have a template for Button in your resource dictionary in the App.xaml file you can access it using the following code:
Application.Current.Resources[typeof(Button)]
If you have merged resource dictionary using code like below
<Window x:Class="MainWindow">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="DefaultStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
</Window>
Then, instead of Application.Current.Resources["ResourceKey"] you need to specify Control name (in this case MainWindow) also like below
var style = Application.Current.MainWindow.Resources["ResourceKey"];
// OR
var style = Application.Current.MainWindow.TryFindResource("ResourceKey");
If you're getting the resources within the same project, try this:
yourControl.Style = FindResource("YourResourceKey") as Style;
Otherwise, try this:
ResourceDictionary res = (ResourceDictionary)Application.LoadComponent(new Uri("/ProjectName;component/FolderName/ResourceDictionaryName.xaml", UriKind.Relative));
yourControl.Style = (Style)res["YourResourceKey"];
You can access a resource dictionary you added to your project as follows:
var rd = new ResourceDictionary();
rd.Source = new Uri("ms-appx:///Dictionary1.xaml");
Then you can access a resource stored in the resource dictionary like so:
someObject.Property = rd["mytemplate"];
NOTE:
You will have to modify the URI to the resource dictionary according to the location you created it relative to the project's base directory.
I found the answer here
https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/how-to-use-a-resourcedictionary-to-manage-localizable-string-resources
create a ressource dictionary "ColorResources.xaml"
add to it:
Blue
edit your app.xml and add:
use the color from your code
var color = (System.Windows.Media.Color)Application.Current.FindResource("ButtonColor1");
and voilĂ 
ps : admin can you fix the code? it does not show up, thanks
Any of the above approaches work getting the resource based on the location, if you are following MVVMm I would recommend doing it this way:
create a Service like ProvideDataTemplateService, (to create a service usual inherit from Behavior )
Use Container of Your choice to inject this service where you would like to have aces to DataTemple.
For the life of me, although I was able to load my resource dictionary via XAML, I wasn't able to load it via "code behind" (in C#).
So I resorted to have a view loading it: (MyView.xaml)
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/My.Proj;component/My/Path/myResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Then access it in my UT by instanciating that view and accessing it:
new MyView().Resources.MergedDictionaries[0]
Hacky, but works.
Just to add another answer here in case you don't have a view or Application.Current is null. I realize this is probably uncommon but in my case I have an addin to a parent application and Application.Current is null; I also want to pass one of my resources to the parent as an ImageSource so I don't have a XAML view created to get resources from directly.
You can also make the dictionary into a code behind creatable object. Just set the x:Class attribute in the XAML and then create a .xaml.cs file in the code behind. Your updated XAML, lets call the code file MyDictionary.xaml, would then look something like this:
<ResourceDictionary 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"
x:Class="Some.Namespace.MyDictionary"
mc:Ignorable="d">
...Resources...
</ResourceDictionary>
And the code behind (MyDictionary.xaml.cs) would look something like this:
using System.Windows;
namespace Some.Namespace
{
public partial class MyDictionary : ResourceDictionary
{
public MyDictionary()
{
InitializeComponent();
}
}
}
Don't forget to call InitializeComponent() as that's what loads the resources. Actually not sorry, see edit below
After you do this you can simply construct an instance of the class anywhere in code and reference the resources by key like this:
var dictionary = new MyDictionary();
var resource = dictionary["whateverKey"] as WhateverResourceType;
Thanks to this post for leading to the idea.
EDIT
Just ran into one potential issue with this. I got a 'Cannot re-initialize ResourceDictionary instance' exception with this setup on some of my controls. On further research this could be related to calling InitializeComponent in the constructor. Instead I removed the constructor from the code behind and added a static method to get an initialized instance as follows:
public static MyDictionary ConstructInitializedInstance()
{
var dictionary = new MyDictionary();
dictionary.InitializeComponent();
return dictionary;
}
You could also just create and initialize in your code behind.

What is the difference between Application.Current.Resources and Resources

I have global ResourceDictionary defined in my App.xaml file.
<Application.Resources>
<ResourceDictionary>
<Style x:Key="yolo" TargetType="Grid" />
</ResourceDictionary>
</Application.Resources>
I've had set breakpoint in random Page and checked difference in debugger:
Application.Current.Resources = 1
Resources = 0
What's the this.Resources?
Application.Current.Resources
Contains the resources that are declared in the App.xaml file, which can be seen the entire application.
this.Resources
Contains the resources that are defined locally for any control, such as Window, UserControl and are only available within this control.
Example with Window:
XAML
<Window ...
xmlns:sys="clr-namespace:System;assembly=mscorlib"
<Window.Resources>
<sys:String x:Key="MyString">TestString</sys:String>
</Window.Resources>
Code-behind
public MainWindow()
{
InitializeComponent();
string test = this.Resources["MyString"] as string;
}
What's the this.Resources?
It's the resource dictionary of the control whose code-behind code we are writing in.
On the other hand Application.Current.Resources is the resource dictionary of the application object itself.
If you are writing code inside App.xaml.cs, then Resources and Application.Current.Resources will refer to the same object.

how to create skins for a GUI in WPF?

I was wondering how I should go about creating custom settings for all the color schemes and such. I know I can create styles for individual components or parts..but how should I create a set of skins?
for example, right now I'm using a maroon colored gradientbrush in a lot of subcontrols. However, I'm sure that people other than me will hate the color scheme.
I know that I can create a dependency property on my top level control for the color and then bind the individual parts that need that color to that dependency property. However, there will need to be many properties. Should I just create a separate style object that contains all these properties and place it as field in my user control?
I'm just wondering if there are other ways of doing this in WPF. For example, I guess there could be some way of doing this in xaml or utilizing some built in class in the default libraries.
You can do this by creating new resource dictionary and define there colors and control templates for your controls.
Example you can find in WPF Themes project (download link).
You can change your style by changing resource dictionary, e.g:
<Application x:Class="ThemesSample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<ResourceDictionary Source="ExpressionDark.xaml"/>
</Application.Resources>
</Application>
If you want to change theme at runtime you should use following code:
ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri("BureauBlack.xaml", UriKind.Relative);
this.Resources.MergedDictionaries.Add(dict);

How to change XAML value using C#?

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);

Use difference XAML pages in XBAP

How would I go about switching the main display XAML page that gets used in an XBAP. The only different is I want a larger mode and a smaller mode, but with the same controls (some hidden).
In your App.xaml.cs file, you can programmatically change which Window.xaml file you want to show on startup. Here is an over-simplified example.
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
System.Windows.Window startupWindow = null;
if(useLargeMode)
{
startupWindow = new LargeMainWindow();
}
else
{
startupWindow = new SmallMainWindow();
}
window.Show();
}
You can also do this by changing the StartupUri in your App.xaml file but that is obviously going to be harder to change at runtime.
<Application x:Class="Main.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml" /> <!-- change this -->
I've haven't tried binding to a property in the application declaration in the XAML, but VS 2010 doesn't complain about this. My concern would be that the app had it's datacontext set early enough for this to work correctly. Give it a try and let us know how it works. :)
<Application x:Class="Main.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="{Binding StartupWindow}">

Categories

Resources