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); }
}
}
现在我数据绑定,为一个标签控件使用下面的代码为什么不能将WPF绑定到UserControl属性?
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;
}
}
的XAML选项卡控件看起来像这样
<TabControl Name="TabControlViews">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding MyProperty}"/>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
能正常工作和标签“一”,选项卡控件上显示“两个”。但是,如果我将Meh的基类型从DependencyObject更改为UserControl,则选项卡为空。为什么是这样?