Hello all,
I have been wrestling with the issue of getting databinding to work, hope that somebody can help me out here.
I have a ListBox which is bound to an ObservableCollection of T (exposed by the Collection property of my window). T has an associated DataTemplate. Here is the simplified version of my app:
<
Window.Resources><
DataTemplate x:Key="StartTemplate"><
Button Command="local:BatchRunnerCommands.StartCommand" CommandParameter="{Binding}" /></
DataTemplate><
DataTemplate x:Key="StopTemplate"><
Button Command="local:BatchRunnerCommands.StopCommand" CommandParameter="{Binding}" /></
DataTemplate><
DataTemplate DataType="{x:Type local:MyType}"><
ContentPresenter Content="{Binding}" ContentTemplateSelector="{StaticResource StatusTemplateSelector}" /></
DataTemplate></
Window.Resources><
ListBox ItemsSource="{Binding Collection}" Width="Auto"></
ListBox>My DataTemplate has a ContentPresenter which looks at my business object and returns a specific Sub-DataTemplate, which in turn is displayed.
So far, so good. But if you look closely at the Content binding of the ContentPresenter, it is bound directly to the business object. The reason that I did this was that in my sub-DataTemplate (called StartTemplate and StopTemplate) I have a Button which has the CommandParameter bound to my business object. Using this approach I can easily perform actions on my business object in my event handlers.
{
BusinessObject obj = (BusinessObject) e.Parameter;obj.DoSomething();
obj.SomeProperty = "blah"; // No refresh here
}
The problem is that if I change a property in my business-object, the ContentPresenter will not re-evaluate the bound business object. I do have an INotifyPropertyChanged but since the ContentPresenter looks directly at the business object (and not at a property, that is done by the sub-DataTemplate) it is simply ignored (by design).
I tried cooking up a solution by binding the CommandParameter property of the button to the RelativeSource, but that doesn't seem to work.
I also thought about calling UpdateSource on the ContentPresenter directly, but as I don't have a reference to this in my command handler this is also kind of trick.
Does anybody have an idea about how to either get the Button to bind to the businessobject and have the ContentPresenter look at a property (with change notification); or force the ConcentPresenter to re-evaluate it's binding in some other way
Thanks,
Waseem