none
Binding problem in DataTemplate RRS feed

  • Question

  • Hello everyone!
    I'm a software developer from Hungary, recently using WPF. Unfortunatelly I'm stuck with a problem concerning data binding in data templates. I've created a simplified sample to represent the issue:

    There is a class called Source which has two string-typed properties defined, which I'd like to present on a window. It is declared like this:

    public class Source
    {
       public string AText { get; set; }
       public string BText { get; set; }
    }

    The ui based on mvvm and having two usercontrols. The first one called HostControl, because it has a dependency property intended to contain the included control:

    public partial class HostControl : UserControl
    {
        public static readonly DependencyProperty IncludedProperty = DependencyProperty.Register(
          "Included",
          typeof(object),
          typeof(HostControl)
        );
         public object Included
        {
            get { return GetValue(IncludedProperty); }
            set { SetValue(IncludedProperty, value); }
        }
         public HostControl()
        {
            InitializeComponent();
        }
    }

    This control also has to display one of the properties in the source:

    <UserControl x:Class="BindingTester.HostControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="root">
        <StackPanel Orientation="Vertical" Background="LightYellow">
            <TextBlock Text="{Binding AText}" />
            <Frame Content="{Binding ElementName=root, Path=Included}" />
        </StackPanel>
    </UserControl>

    The included control only contains a textblock to display the second source property:

    <TextBlock Text="{Binding BText}" />

    The goal is to connect the peaces from the window's xaml, like this:

    <ContentControl>
        <ContentControl.Content>
            <local:Source AText="atext" BText="btext" />
        </ContentControl.Content>
        <ContentControl.ContentTemplate>
            <DataTemplate>
                <local:HostControl DataContext="{Binding}">
                    <local:HostControl.Included>
                        <local:IncludedControl DataContext="{Binding}" />
                    </local:HostControl.Included>
                </local:HostControl>
            </DataTemplate>
        </ContentControl.ContentTemplate>
    </ContentControl>

    Unfortunatelly the included control's datacontext gets only be set to 'null'.

    I've tried to define x:name for the host control, or the datatemplate itself and bind to them, no luck so far, every binding attempts results 'null'.

    Do you have any idea why is this? Any help would be highly appreciated.
    Monday, February 1, 2010 4:01 PM

Answers

  • I've found the answer. It seems that the data context inheritance doesn't work in Frame control. Replacing the Frame control to e.g. ContentControl solves the problem.
    • Marked as answer by kataik Monday, February 1, 2010 5:04 PM
    Monday, February 1, 2010 5:04 PM