【发布时间】:2014-07-23 09:36:57
【问题描述】:
我已经实现了一个用户控件,可以让我快速构建几个类似的界面屏幕。基本上它定义了两个依赖属性MainContent 和UserInteractions,然后显示在可视化模板(ResourceDictionary 中的 xaml)中,如下所示:
+-------------+
| L | |
| o | Main |
| g | Content |
| o | |
+---+---------+
| Interaction |
+-------------+
屏幕的 Xaml 如下所示:
<controls:ScreenControl>
<controls:ScreenControl.MainContent>
<TextBlock>Some content goes here</TextBlock>
</controls:ScreenControl.MainContent>
<controls:ScreenControl.UserInteractions>
<Button>Do something</Button>
</controls:ScreenControl.UserInteractions>
</controls:InstallerScreenControl>
当我运行应用程序时,这很好用。但是,在设计器中,什么都看不见。不是视图中明确定义的内容,也不是模板中的内容。我需要添加什么来启用设计支持?我尝试按照某些地方的建议将模板移动到Themes/Generic.xaml,但这没有任何区别。 This SO question 似乎相关,但没有得到有用的答案。
编辑:
我的ScreenControl 看起来像这样:
public class ScreenControl : UserControl
{
public object MainContent
{
get { return GetValue(MainContentProperty); }
set { SetValue(MainContentProperty, value); }
}
public static readonly DependencyProperty MainContentProperty = DependencyProperty.Register(
name: "MainContent",
propertyType: typeof(object),
ownerType: typeof(ScreenControl),
typeMetadata: new PropertyMetadata(default(object)));
public object UserInteractions
{
get { return GetValue(UserInteractionsProperty); }
set { SetValue(UserInteractionsProperty, value); }
}
public static readonly DependencyProperty UserInteractionsProperty = DependencyProperty.Register(
name: "UserInteractions",
propertyType: typeof(object),
ownerType: typeof(ScreenControl),
typeMetadata: new PropertyMetadata(default(object)));
}
当在设计器中查看使用该控件的屏幕时,它只显示:
即,什么都没有,只有一个空白框。
使用控件时,我创建了一个UserControl,添加了问题开头所示的 Xaml,并删除了代码隐藏文件。
【问题讨论】:
标签: c# wpf xaml user-controls