3

I have this XAML:

<MenuItem Name="celsiusBtn"
          Header="{Binding Path=celsiusBtn, Source={StaticResource Resources}}" 
          IsChecked="True"
          Click="celsiusBtn_Click" />

I'm using this to bind string and be able to change them at runtime:

WPF Runtime Localization

Now my problem is I need to do the same binding in code, but I don't know how to specify the Source in the Binding. I know I can give the Path or the property name in the constructor of the class, but I'm unsure on how to access the Source property or even to define it to the StaticResource.

1
  • Is the Resources is static class? Commented Mar 2, 2014 at 4:42

1 Answer 1

3

Here is an example of Binding in code for Label.Content property, what I tested:

// analogue of this line: 
// {Binding Path=LabelCultureName, Source={StaticResource Resources}}

var binding = new Binding();
binding.Path = new PropertyPath("LabelCultureName");
binding.Source = (ObjectDataProvider)App.Current.FindResource("Resources");

TestLabel.SetBinding(Label.ContentProperty, binding);

In your case it will be something like this:

var binding = new Binding();
binding.Path = new PropertyPath("celsiusBtn");
binding.Source = (ObjectDataProvider)App.Current.FindResource("Resources");

celsiusBtn.SetBinding(MenuItem.HeaderProperty, binding);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.