XAML设计师在绑定时显示属性名称而不是设计器数据(d:DataContext)

13

背景

我希望在 XAML 设计器上在我的 UserControlRepositoryContainer)中填充数据。

我创建了一个名为RepositoryContainerDesignData.xaml的文件(它和RepositoryContainer.xaml在同一个文件夹中),并将其设置为UserControld:DataContext

但是,XAML 设计器显示属性名称而不是显示数据。

这是一个最简示例:

设计数据 (RepositoryContainerDesignData.xaml)

<local:RepositoryContainer xmlns:local="clr-namespace:SystemSpecs.View.UserControls"
                           Title="My Repository Title"
/>

用户控件 (RepositoryContainer.xaml)

<UserControl x:Class="SystemSpecs.View.UserControls.RepositoryContainer"
                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:SystemSpecs.View.UserControls"
                mc:Ignorable="d" 
                d:DesignHeight="100" d:DesignWidth="500"
                d:DataContext="{d:DesignData Source=RepositoryContainerDesignData.xaml}"
                DataContext="{Binding RelativeSource={RelativeSource Self}}">

    <Grid>
        <TextBlock Text="{Binding Path=Title}" FontSize="24" Foreground="White" HorizontalAlignment="Center" />
    </Grid>
</UserControl>

后台代码

using System.Windows.Controls;

namespace SystemSpecs.View.UserControls
{
    public partial class RepositoryContainer : UserControl
    {
        public string Title { get; set; }

        public RepositoryContainer()
        {
            InitializeComponent();
        }
    }
}

预期输出:

预期输出

输出:

输出

已尝试的方法:

环境信息:

  • Windows 10 Pro x64
  • Visual Studio Community 2015 (版本14.9.25431.01更新3)
  • Microsoft .NET Framework 4.6.01586
  • 更多信息请查看Pastebin RAW以保持帖子整洁

我是否遗漏了什么?

附注

如果我创建一个类(例如public class RepositoryContainerData),创建一个名为Title的属性,并将该类的实例设置为d:DataContextd:DataContext="{d:DesignInstance local:RepositoryContainerData, IsDesignTimeCreatable=True}"),则可以按预期工作。


7
"PS" 部分提供的解决方案是正确的方法。您不需要创建设计用户控件,而是需要创建设计数据。 - Novitchi S
2个回答

3

在WPF用户控件中应该使用依赖属性,因为它们具有更改通知。

    public static DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(RepositoryContainer), new FrameworkPropertyMetadata(new PropertyChangedCallback(Title_Changed)));

    public string Title
    {
        get { return (string)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }


    private static void Title_Changed(DependencyObject o, DependencyPropertyChangedEventArgs args)
    {
        RepositoryContainer thisClass = (RepositoryContainer)o;
        thisClass.SetTitle();
    }

    private void SetTitle()
    {
        //Put Instance Title Property Changed code here
    }

只需将您的标题属性替换为上面的代码,无需添加任何内容即可使其正常工作。如果您需要在代码中对标题更改做出反应,则SetTitle()仅需要代码。

我有一个很久以前创建的c#代码片段,可以轻松创建依赖属性,如果您需要,请告诉我。


3

您尝试做的事情对我来说可能需要一些小改动才能实现。我创建了一个名为WpfApplication2的新应用程序。

RepositoryContainerDesignData.xaml包含:

<wpfApplication2:RepositoryContainer 
    xmlns:wpfApplication2="clr-namespace:WpfApplication2"
    Title="My repository title">
</wpfApplication2:RepositoryContainer>

文件属性已设置为: enter image description here 控件定义为:
<UserControl x:Class="WpfApplication2.RepositoryContainer"
             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"
             d:DataContext="{d:DesignData Source=RepositoryContainerDesignData.xaml}"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid Background="Bisque">
        <TextBlock Text="{Binding Path=Title}" FontSize="24" 
                   HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid>
</UserControl>

在设计师中的结果是:

enter image description here

虽然这不是我通常会做的事情,但我学到了一些东西 :)


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