Question

In my program I would like to implement a DynamicResource from code-behind. Right now I am binding the Content of a Label to a string property in my Data Model...

<Label Content="{Binding DataModel.StringValue}" ... />

Following this question, I have implemented the string in my Data Model like so:

private string _stringValue = (string)Application.Current.Resources["nameOfResource"];
public string StringValue
{
    get { return _cartsInSystem; }
    set
    {
        _cartsInSystem = value;
        NotifyPropertyChange(() => CartsInSystem);
    }
}

I would like to make it so that every time the user changes the Resource Dictionary, this string value updates with the new value.

I am trying to achieve the same effect as something like this:

<Label Content="{DynamicResource nameOfResource}" ... />

Please let me know what I am doing wrong, and how I might correctly implement something like this.

UPDATE 1: As requested by @HighCore, this is an example of my code where I only have access to string values from code-Behind (or C# class)

(This is part of a ViewModel of a TreeView in my MainWindow)

//The "DisplayNames" for these nodes are created here and not accessible through xaml.
//This is because the xaml window has access to this code through it's itemsSource
private HierarchicalVM CreateCartsNode()
{   
    return new HierarchicalVM()
    {
        DisplayName = "Carts",
        Children = 
        { 
            new CartConnection() { ConnectionDataModel = new CartConnectionModel(), DisplayName = "Cart Connection" }, 
            new HierarchicalVM() {
                DisplayName = "Cart Types",
                Children = {
                    CreateCartType( new CartConfigModel() { DisplayName = "Default" }, new CartIO_Model() ),
                },
                Commands = { new Command(OpenAddCart) {DisplayName = "Add..."} }
            }
        }
    };
 }

This is the xaml of the above TreeView:

<!-- Tree view items & Functions -->
<TreeView ItemsSource="{Binding DataTree.Data}" ... />

Update 2: I have another perfect example of my problem...

I have a comboBox that has it's itemsSource bound to an ObservableCollection in my Data Model. Like so:

private ObservableCollection<string> _objCollection;
private string _notUsed = "Not Used";
private string _stop = "Stop";
private string _slow = "Slow";

public DataModel()
{
    ObjCollection = new ObservableCollection<string>() { _notUsed, _stop, _slow };
}

public ObservableCollection<string> ObjCollection {...}

xaml:

<ComboBox ItemsSource="{Binding DataModel.ObjCollection}" ... />

If I want to make it so that the items in this comboBox change when the resource dictionary is changed, it looks like I'll need to handle it in C# rather than xaml.

Was it helpful?

Solution

After OP's UPDATE 2 and having a chat with him for a different question, I understood he was trying achieve localisation for his application. He would change Resource Dictionaries (for different languages) on the fly, and he wanted his C# code re-read/load values from Application.Current.Resources.

APPROACH ONE

After you changing the Resource Dictionary, You could use something like EventAggregator/Mediator to let other parts of the application (including ViewModels) know about Resource Dictionary change, and they respond to it by re-loading/reading resources/values from Application.Current.Resources

APPROACH TWO

OP doesn't want to introduce any new dependencies like EventAggregator/Mediator. So, I suggested this second approach. I know, it is not pretty, but here it goes..

You could have a global static event instead of EventAggregator/Mediaotr to let other parts of the application know that you swapped resource dictionary, and they will re-load/read values.

Read this answer about potential problems with static events and their subscriptions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top