I have a trivial user control:
<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>
and the code behind:
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)); } }
I naively expect the VS2010 constructor for UserControl to display my default metadata values โโfor my two controls - โhelloโ in one text block, and today the date and time in another, but they are empty.
If I compile and drop the control into a WPF application, it will look great, but for now in the xaml project / designer of the UserControl project.
I tried changing the datacontext around, binding differently, implementing OnPropertyChanged, etc., but nothing does the data in the project view of the UserControl project.
Does anyone know the answer to this question? I searched around, and either itโs so obvious that I miss it, or itโs the way it is.