我有一个简单的用户控件:
<UserControl x:Class="Xxx.SimpleUserControl.SimpleTextUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="root">
<StackPanel Background="Blue">
<TextBlock x:Name="TitleTextBlock" Text="{Binding ElementName=root, Path=Title}" Background="White" Width="200" Height="30" Margin="5" />
<TextBlock Text="{Binding ElementName=root, Path=Time}" Background="White" Width="200" Height="30" Margin="9" />
</StackPanel>
</UserControl>
以及背后的代码:
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
namespace Xxx.SimpleUserControl
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class SimpleTextUserControl : UserControl
{
public SimpleTextUserControl()
{
InitializeComponent();
}
[Browsable(true)]
[Category("SimpleControl")]
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
// Using a DependencyProperty as the backing store for Title. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(string), typeof(SimpleTextUserControl), new FrameworkPropertyMetadata("hello"));
[Browsable(true)]
[Category("SimpleControl")]
public DateTime Time
{
get { return (DateTime)GetValue(TimeProperty); }
set { SetValue(TimeProperty, value); }
}
// Using a DependencyProperty as the backing store for Time. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TimeProperty =
DependencyProperty.Register("Time", typeof(DateTime), typeof(SimpleTextUserControl), new UIPropertyMetadata(DateTime.Now));
}
}
我天真地期望VS2010 UserControl的设计者能够显示我两个控件的默认元数据值 - 一个文本块中的“hello”,另一个中显示今天的日期和时间,但它们是空的。
如果我编译并将控件放入WPF应用程序中,它会正常渲染,但在UserControl项目xaml视图/设计器中不会。
我尝试过改变数据上下文,以不同的方式绑定,实现OnPropertyChanged等,但没有任何方法可以使数据在UserControl项目的设计视图中呈现。
有人知道这个问题的答案吗? 我已经搜索了一些资料,但要么显而易见我错过了,要么就是“就是这样”。