Quantcast
Channel: Binding problem in DataTemplate
Viewing all articles
Browse latest Browse all 2

Binding problem in DataTemplate

$
0
0
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.

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images