【问题标题】:XAML Designer displays property name instead of Designer Data (d:DataContext) when BindingXAML 设计器在绑定时显示属性名称而不是设计器数据 (d:DataContext)
【发布时间】:2017-06-25 01:30:37
【问题描述】:

上下文

我希望我的UserControl (RepositoryContainer) 在 XAML 设计器上填满数据。

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

但 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 专业版 x64
  • Visual Studio 社区 2015(版本 14.9.25431.01 更新 3)
  • Microsoft .NET Framework 4.6.01586
  • Pastebin RAW 上的更多信息以保持帖子干净

我错过了什么吗?

PS

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

【问题讨论】:

  • “PS”部分的解决方案是正确的方法。您无需创建设计用户控件,而是创建设计数据。

标签: c# wpf visual-studio xaml


【解决方案1】:

您应该在 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
    }

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

我有一个很久以前创建的c#代码sn-p,它可以很容易地制作依赖属性,如果你想要的话,请告诉我。

【讨论】:

    【解决方案2】:

    您尝试做的事情可能对我有用,可能需要进行一些细微的更改。我创建了一个名为 WpfApplication2 的新应用。

    RepositoryContainerDesignData.xaml 包含:

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

    文件属性设置为:

    控件定义为

    <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>
    

    设计器中的结果是

    我通常不会这样做,但我学到了一些东西:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      相关资源
      最近更新 更多