views:

992

answers:

1
public class Meh : DependencyObject
{
    public static readonly DependencyProperty MyPropertyProperty =
     DependencyProperty.Register("MyProperty", typeof(string), typeof(Meh));

    public string MyProperty
    {
     get { return (string)GetValue(MyPropertyProperty); }
     set { SetValue(MyPropertyProperty, value); }
    }
}

Now I databind that to a tab control using the following code

public partial class Window1 : Window
{
    public Window1()
    {
     InitializeComponent();
     var data = new List<Meh>();
     data.Add(new Meh { MyProperty = "One" });
     data.Add(new Meh { MyProperty = "Two" });
     TabControlViews.ItemsSource = data;
    }
}

The XAML for the tab control looks like this

<TabControl Name="TabControlViews">
    <TabControl.ItemTemplate>
     <DataTemplate>
      <TextBlock Text="{Binding MyProperty}"/>
     </DataTemplate>
    </TabControl.ItemTemplate>
</TabControl>

This works fine and the tabs "One", "Two" appear on the tab control. However if I change the base type of Meh from DependencyObject to UserControl the tabs are blank. Why is this?

+2  A: 

EDIT

I've checked your code, and it seems that at runtime, the ContentPresenter of the TabItem does not inherit the DataContext from the TabControl as soon the item to display is of type Visual or derived from it.

This smells like some TabControl funny behaviour, since replacing TabControl with ListBox works fine. As to the specific cause of the problem, it isn't apparent to me.

kek444