Home » WPFRSS

How to databind the DataContext of a ContentPresenter's Content?

I have a ContainerWindow that has a ContentPresenter inside of it, defined using XAML. I have added a dependency proparty named ChildControl inside ContainerWindow so it can be databound to the ContentPresenter.Content.

Then I have the class ContainerWindowViewModel that contains a property named ChildControlViewModel,  and I would like to databind that property to the ContentPresenter.Content's DataContext. Is it possible to express this in XAML or does it have to be done in the code behind?

Thanks,

 

XAML:

 

<UserControl x:Class="MyNamespace.ContainerWindow">
    <ContentPresenter Content="{Binding RelativeSource={RelativeSource Self}, Path=ChildControl}" />
</UserControl>

 

ViewModel:

 

class ContainerWindowViewModel
{
    public object ChildControlViewModel
   {
        get { [...] }
        set { [...] }
    }
}

 

 

 

2 Answers Found

 

Answer 1

Hi Sylvain,

There is a mistake in your code,
  <ContentPresenter Content="{Binding RelativeSource={RelativeSource Self}, Path=ChildControl}" />
the "RelativeSource Self" references to the ContentPresenter self; but as you said, the ContentPresenter does not have the property  ChildControl. So the correct is 
  <ContentPresenter Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type l:ContainerWindow}}, Path=ChildControl}" />

And you can create a ContainerWindowViewModel instance in xaml  by resources, then bind ContentPresenter.DataContext to this static resource.

code snippet:

<UserControl x:Class="MyNamespace.ContainerWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Height="300" Width="300"
  xmlns:l="clr-namespace:MyNamespace">
 <UserControl.Resources>
  <l:ContainerWindowViewModel x:Key="containerWindowViewModel"/>
 </UserControl.Resources>
 <ContentPresenter DataContext="{Binding Source={StaticResource containerWindowViewModel}, Path=ChildControlViewModel}"
          Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type l:ContainerWindow}}, Path=ChildControl}" />
</UserControl>

If you have any problem, please feel free to let me know.

Sincerely,

Bob Bao

 

Answer 2

Is there a way to make this work with a data context that would not be created as a static resource by XAML?

When I set  a Data context binding in my ContentPresenter, the binding appears to be ignored.

When I do the following:

<ContentPresenterDataContext="{Binding ChildControlViewModel}"Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type l:ContainerWindow}}, Path=ChildControl}"/>

I get binding errors for my child control as it looks for the properties in the ContainerWindowViewModel instead of the ChildControlViewModel. If I remove the binding:

<ContentPresenterContent="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type l:ContainerWindow}}, Path=ChildControl}"/>

I get the same result. So it is as if binding this way would do nothing in my code.

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter