【发布时间】:2014-04-29 11:43:23
【问题描述】:
我有一些在设计器中显示良好的用户控件,但我无法从构造函数对设计时示例内容进行任何更改。好像根本没有执行。
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>
代码:
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!";
}
}
}
我尝试了很多选项,但仍然不知道如何在 WPF 设计器中显示设计时数据:(不同的上下文绑定也没有显示任何内容...
PS: 在 Win8 上尝试过干净的 VS2012 和 VS2013 项目。没有任何效果! :( 我不知道该怎么办,在网上还没有找到类似的东西......只在构造函数中添加设计检查并设置现有控件文本就足够了吗?它应该可以工作,对吧?
【问题讨论】:
-
你在使用任何框架吗?你想达到什么目标?
-
您的代码中可能存在仅在设计模式下出现的错误。要解决此类问题,您可以按照 MSDN 上描述的过程:msdn.microsoft.com/en-us/library/ee856616.aspx。您甚至可以在
Test构造函数中设置断点,以查看Text属性是否已正确分配。 -
我没有使用任何特定的东西,只是一个普通的 WPF。我正在测试没有任何资源的控件。我试图附加,但似乎构造函数甚至没有运行,在设计器中控制负载并且没有捕获任何内容(断点已填充,因此源加载正常)。也许某些 VS 设置失败了?我不知道......在VS2012中这样做,一切都很好。也可能是Win8问题?看不出我的代码有什么问题。
-
我对 VS2013 也有同样的问题。我可以设置启用 Binding Intellisense 的设计器数据上下文,但未显示设计器数据。如果我找到解决方案,我会通知您。
标签: c# wpf visual-studio-2013