Custom Binding Extension -- how to access source object.?
-
Using this excellent article[^] I am trying to set up a custom binding extension. The issue is that when the binding finds the source object, I need to do some stuff to the source object, but I don't know where to put that code. For example, I have the following class:
class MyObject {
public string Value { get; set; }
}and some XAML in MainWindow.xaml:
<Button Content="{my:CustomBinding Value}">
And set the data context in code:
public MainWindow()
{
InitializeComponent();
this.DataContext = MyObject;
}That much works, the Value string gets displayed on the button. But what I need to do is write code in the CustomBinding class that operates on MyObject as soon as possible, which means I need to resolve MyObject from the BindingExpression. What I've figured out so far is that when the CustomBinding instance is created, it doesn't actually find the MyObject.Value because it needs to find the data context first in order to resolve the BindingExpression, so it doesn't actually find MyObject until the MainWindow gets laid out on screen. So my question is, what code do I write in the CustomBindingExtension class so that it runs when the WPF layout system decides to resolve the BindingExpression to find MyObject?
-
Using this excellent article[^] I am trying to set up a custom binding extension. The issue is that when the binding finds the source object, I need to do some stuff to the source object, but I don't know where to put that code. For example, I have the following class:
class MyObject {
public string Value { get; set; }
}and some XAML in MainWindow.xaml:
<Button Content="{my:CustomBinding Value}">
And set the data context in code:
public MainWindow()
{
InitializeComponent();
this.DataContext = MyObject;
}That much works, the Value string gets displayed on the button. But what I need to do is write code in the CustomBinding class that operates on MyObject as soon as possible, which means I need to resolve MyObject from the BindingExpression. What I've figured out so far is that when the CustomBinding instance is created, it doesn't actually find the MyObject.Value because it needs to find the data context first in order to resolve the BindingExpression, so it doesn't actually find MyObject until the MainWindow gets laid out on screen. So my question is, what code do I write in the CustomBindingExtension class so that it runs when the WPF layout system decides to resolve the BindingExpression to find MyObject?