3

Is it possible to set the x:Reference Markup extension for a binding in code?

For example:

{Binding SelectedItem, Source={x:Reference ComboList}}

So i create a binding like this:

Binding b = new Binding("SelectedItem");
b.Source = //What to put here??

I was previously using ElementName but I had some issues with the NameScope as referred in this question because this binding is set in a ComboBox which is inside of a UserControl I created, and apparently if I use ElementName the namescope is limited only to that UserControl and not outside..

Thanks!

Community
  • 1
  • 1
Adolfo Perez
  • 2,834
  • 4
  • 41
  • 61
  • Why are you creating a binding in code to begin with? Post a screenshot of what you need. – Federico Berasategui Mar 20 '13 at 16:35
  • Because I'm building a WPF Form designer where user drags and drops controls to a design surface and sets bindings through a property window. I use the binding info in the Properties Window to set those bindings programmatically. – Adolfo Perez Mar 20 '13 at 16:37
  • `{x:Reference}` is a XAML construct, it's not available in C# code. – Federico Berasategui Mar 20 '13 at 16:38
  • Then how can I refer to an `ElementName` outside of my UserControl boundaries if I'm creating the binding in code? – Adolfo Perez Mar 20 '13 at 16:40
  • You can't. That doesn't make sense. If you need something inside the `UserControl` to have a property from the outside, you have to create that property as a `DependencyProperty` within the `UserControl` itself, then bind your element to that property via `RelativeSource`. – Federico Berasategui Mar 20 '13 at 16:42
  • @HighCore is more right than not - while you can go down the route of trying to manually new up a `System.Windows.Markup.Reference` instance, force it to generate a value via `ProvideValue`, etc., it's a rats-nest you *don't* want to go down. – JerKimball Mar 20 '13 at 16:54
  • @HighCore, "then bind your element to that property via RelativeSource" How can I bind using `RelativeSource` if I still need to do a Name lookup to locate my source Control? RelativeSource only allows specifying Ancestor Types/Levels as far as I know – Adolfo Perez Mar 20 '13 at 17:06
  • 1
    I mean, the DP should be in the `UserControl`, right? then you can `"{Binding MyProp, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"` – Federico Berasategui Mar 20 '13 at 17:08
  • Genius, that works!, if you enter an Answer for that I will mark it . Thanks for your help :) – Adolfo Perez Mar 20 '13 at 17:11

1 Answers1

3

Converting my comments into an answer:

{x:Reference} is a XAML construct, it's not available in C# code

If you need something inside the UserControl to have a property from the outside, you have to create that property as a DependencyProperty within the UserControl itself, then bind your element to that property via RelativeSource:

In the UserControl code behind:

 public static readonly DependencyProperty SomeProperty = DependencyProperty.Register("Some", typeof (SomeValue), typeof (YourUserControl), new PropertyMetadata(default(SomeValue)));

        public SomeValue Some
        {
            get { return (SomeValue) GetValue(SomeProperty); }
            set { SetValue(SomeProperty, value); }
        }

Somewhere within the UserControl Visual Tree:

<TextBox Text="{Binding Some, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"/>
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154