tags:

views:

999

answers:

1

Hi,

For some user controls, I have this binding:

            AppLanguage="{Binding Path=ApplicationLanguage, Source={x:Static Application.Current}}"

This works for controls that are declared/instantiated in XAML. However, I have a control that is only instantiated dynamically (it won't be used regularly, so I don't want an instance (up to 3, actually) to gobble up memory for nothing all the time. Now, unless I'm missing something, I have to declare my bindings in code-behind. That works fine when I have an easy one (ElementName + Path), but in the above case, I can't figure out how to write it in code-behind.

Of course, in this particular case, the control could simply refer to My.Application.ApplicationLanguage, but trying to do this got me curious anyway. I did a good number of searches and couldn't find anything similar (might be my search keywords though. :))

Thanks, Michel

A: 

x:Static just resolves a static member for you, so you can write as:

var binding = new Binding("ApplicationLanguage");
binding.Source = Application.Current;

That said, I don't follow why you think you need to do this in code.

HTH, Kent

Kent Boogaart
That work - thanks. I was putting Application.Current between double-quotes (actually I don't know if that would have worked - I had another problem caused by an event - all fixed now.)I'm doing this in code-behind because (I think) if I put this in XAML in the container, the user control is instantiated no matter what. I may be missing something here - I'm still exploring WPF so some things aren't always obvious. :)
MetalMikester