Does VS2010 display data in UserControls at design time?

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.

+4
source share
1 answer

I think you need to use the DataContext "DesignTime". Add the following to your UserControl Xaml file

 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:YourNamespace" mc:Ignorable="d" 

And then set the DataContext DesignTime with

 d:DataContext="{d:DesignInstance local:SimpleTextUserControl, IsDesignTimeCreatable=True}" 

And remove the ElementName from the Bindings

 <UserControl x:Class="YourNamespace.SimpleTextUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:YourNamespace" mc:Ignorable="d" x:Name="root" d:DataContext="{d:DesignInstance local:SimpleTextUserControl, IsDesignTimeCreatable=True}"> <StackPanel Background="Blue"> <TextBlock x:Name="TitleTextBlock" Text="{Binding Path=Title}" Background="White" Width="200" Height="30" Margin="5" /> <TextBlock Text="{Binding Path=Time}" Background="White" Width="200" Height="30" Margin="9" /> </StackPanel> </UserControl> 

If you still have problems with the work, I downloaded a small sample project, which you can compare with: http://www.mediafire.com/?gan28oeel4qf7ik

+4
source

Source: https://habr.com/ru/post/1340528/


All Articles