简体   繁体   English

不显示Visual Studio 2013 WPF设计时数据

[英]Visual Studio 2013 WPF design-time data isn't displayed

i have some UserControls that are shown fine in designer, but i can't make any changes to the design-time example content from the constructor. 我有一些在设计器中可以正常显示的UserControl,但是我无法对构造函数中的设计时示例内容进行任何更改。 It seems like it is not executed at all. 似乎根本没有执行。

XAML: XAML:

<UserControl x:Class="Example.Test"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
        <TextBlock Name="testx" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>

Code: 码:

using System.ComponentModel;
using System.Windows.Controls;

namespace Example
{
    /// <summary>
    /// Interaction logic for Test.xaml
    /// </summary>
    public partial class Test : UserControl
    {
        public Test()
        {
            InitializeComponent();
            if (DesignerProperties.GetIsInDesignMode(this))
                testx.Text = " IN DESIGN!";
        }
    }
}

I've tried many options, but still can't get it how to display design-time data in WPF designer :( Different context binding also shows nothing... 我已经尝试了很多选项,但是仍然无法在WPF设计器中显示如何显示设计时数据:(不同的上下文绑定也什么也没有显示...

PS: Tried clean VS2012 and VS2013 projects on Win8. PS:在Win8上尝试了干净的VS2012和VS2013项目。 NOTHING WORKS! 什么都没做! :( I don't know what to do, haven't found anything similar on the net... Is it sufficient to just add design check in constructor and set existent control text? It should work, right? :(我不知道该怎么办,还没有在网上找到任何类似的东西...仅在构造函数中添加设计检查并设置现有的控制文本就足够了吗?

K, the short answer is: You're on the right path. K,最简单的答案是:您在正确的道路上。
The long one is: It's a bit more complicated than that. 总的来说是:比这复杂一点。

Your example will "kinda" work, as in, if you'll put an else testx.Text = RUNTIME; 如果放入另一个else testx.Text = RUNTIME; ,示例将像“ kinda”一样工作else testx.Text = RUNTIME; after your if , like that: 在您if ,像这样:

if (DesignerProperties.GetIsInDesignMode(this))
   testx.Text= " IN DESIGN!";
else
   testx.Text= " Runtime";

you'll see what you want on runtime , but you're design time will stay empty. 您将在运行时看到所需的内容,但您的设计时间将为空。

For the Design time, you also need to set the context if I'm not mistaken. 在设计时,如果我没记错的话,还需要设置上下文。 If you're using any of the MVVM framework out there, you kinda get this functionality for "free". 如果您正在使用任何MVVM框架,则可以“免费”获得此功能。 As in, you'll have a "in design time" property and you can set whatever data you want for the design. 与之类似,您将拥有“设计时”属性,并且可以设置设计所需的任何数据。 The catch is that you need to have an empty constructor if my memory serves me right. 要注意的是,如果我的记忆正确,则需要有一个空的构造函数。 You'll also use bindings, and not set the text property directly. 您还将使用绑定,而不是直接设置text属性。

I remember that the default WPF and binding for design time was lacking a bit last time I tried to do something like that in "vanilla" wpf (as in, no MVVM, no bindings), but I believe that with a bit of a hack it's achievable. 我记得上次我在“香草” wpf中尝试执行类似的操作时缺少默认的WPF和绑定(例如,没有MVVM,没有绑定),但是我相信这有点儿破解这是可以实现的。 Again, can't remember it from the top of my head. 再说一次,我记不起来了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM