Wpf VS2010在设计期间是否在用户控件中显示数据?
我有一个简单的用户控件:Wpf VS2010在设计期间是否在用户控件中显示数据?,wpf,visual-studio-2010,user-controls,designer,Wpf,Visual Studio 2010,User Controls,Designer,我有一个简单的用户控件: <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">
<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));
}
}
使用系统;
使用系统组件模型;
使用System.Windows;
使用System.Windows.Controls;
命名空间Xxx.SimpleUserControl
{
///
///UserControl1.xaml的交互逻辑
///
公共部分类SimpleTextSerControl:UserControl
{
公共SimpleTextSerControl()
{
初始化组件();
}
[可浏览(正确)]
[类别(“SimpleControl”)]
公共字符串标题
{
获取{return(string)GetValue(TitleProperty);}
set{SetValue(TitleProperty,value);}
}
//使用DependencyProperty作为标题的后备存储。这将启用动画、样式、绑定等。。。
公共静态只读从属属性TitleProperty=
Register(“Title”、typeof(string)、typeof(simpletextsercontrol)、newframeworkpropertyMetadata(“hello”);
[可浏览(正确)]
[类别(“SimpleControl”)]
公共日期时间
{
获取{return(DateTime)GetValue(TimeProperty);}
set{SetValue(TimeProperty,value);}
}
//使用DependencyProperty作为时间的后备存储。这将启用动画、样式、绑定等。。。
公共静态只读DependencyProperty TimeProperty=
Register(“Time”、typeof(DateTime)、typeof(simpleTextSerControl)、newUIPropertyMetadata(DateTime.Now));
}
}
我天真地希望UserControl的VS2010设计器为我的两个控件显示默认元数据值——“hello”在一个文本块中,今天的日期和时间在另一个文本块中,但它们是空的
如果我编译该控件并将其放入WPF应用程序中,它会呈现良好效果,但在UserControl项目xaml视图/设计器中不会
我尝试过改变datacontext,以不同的方式绑定,实现OnPropertyChanged等,但是没有任何东西可以让数据呈现在UserControl项目的设计视图中
有人知道这个问题的答案吗?我到处搜索过,要么是因为太明显我没有找到它,要么就是因为“它就是这样”。我认为您需要使用“设计时间”数据上下文。将以下内容添加到
UserControl
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"
然后使用设置DesignTime数据上下文
d:DataContext="{d:DesignInstance local:SimpleTextUserControl,
IsDesignTimeCreatable=True}"
并从绑定中删除ElementName
<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>
如果您在实现这一点上仍然有问题,我上传了一个小样本项目,您可以将其与之进行比较:谢谢。这正是我所缺少的,并为我节省了不少时间。具有讽刺意味的是,我只是把d:DataContext作为一个解决方案,当我回头查看以找到您的答案时。