VS2010在设计时是否显示UserControls中的数据?

4

我有一个简单的用户控件:

<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项目的设计视图中呈现。
有人知道这个问题的答案吗? 我已经搜索了一些资料,但要么显而易见我错过了,要么就是“就是这样”。
1个回答

4
我认为你需要使用“DesignTime” DataContext。将以下内容添加到您的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"

然后使用以下代码设置设计时 DataContext

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>

如果您仍然无法使其工作,我上传了一个小的示例项目供您参考:http://www.mediafire.com/?gan28oeel4qf7ik

谢谢。这就是我所缺少的,它为我节省了相当多的时间。具有讽刺意味的是,当我回来查看您的答案时,我刚好在考虑d:DataContext作为解决方案。 - Barry

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接