如何在 WPF 设计器中禁用自定义窗口渲染?
我创建了窗口派生类(WindowAttachedCollection.MyWindow)和附加属性来保存这些窗口的集合。但是 VS 2010 中的 WPF 设计器尝试为该集合中的每个窗口创建 WindowInstance 对象,并抛出 ArgumentException:
值“Microsoft.Expression.Platform.WPF.InstanceBuilders.WindowInstance”不是“WindowAttachedCollection.MyWindow”类型,无法使用在这个通用集合中。 参数名称:值
因此它破坏了WPF设计器。
有什么方法可以在 WPF 设计器中禁用实例化 WindowInstance 而不是 MyWindow 吗?目前,我不需要任何对此 MyWindow 集合的设计时支持。
编辑:
public static readonly DependencyPropertyKey DialogsPropertyKey = DependencyProperty.RegisterAttachedReadOnly(
"DialogsInternal",
typeof(ObservableCollection<MyWindow>),
typeof(MyWindow),
new PropertyMetadata(null));
public static readonly DependencyProperty DialogsProperty = DialogsPropertyKey.DependencyProperty;
public static void SetDialogs(UIElement element, ObservableCollection<MyWindow> value)
{
element.SetValue(DialogsPropertyKey, value);
}
public static ObservableCollection<MyWindow> GetDialogs(UIElement element)
{
var dialogs = (ObservableCollection<MyWindow>)element.GetValue(DialogsProperty);
if (dialogs == null)
{
dialogs = new ObservableCollection<MyWindow>();
SetDialogs(element, dialogs);
}
return dialogs;
}
I have created window derived class (WindowAttachedCollection.MyWindow) and attached property which holds collection of these windows. But WPF designer in VS 2010 tries to create WindowInstance object for each window in that collection and it throws ArgumentException:
The value "Microsoft.Expression.Platform.WPF.InstanceBuilders.WindowInstance" is not of type "WindowAttachedCollection.MyWindow" and cannot be used in this generic collection.
Parameter name: value
So it breaks WPF designer.
Is there any way how to disable instancing WindowInstance instead of MyWindow in WPF designer? At this time I don't require any design-time support for this collection of MyWindow.
EDIT:
public static readonly DependencyPropertyKey DialogsPropertyKey = DependencyProperty.RegisterAttachedReadOnly(
"DialogsInternal",
typeof(ObservableCollection<MyWindow>),
typeof(MyWindow),
new PropertyMetadata(null));
public static readonly DependencyProperty DialogsProperty = DialogsPropertyKey.DependencyProperty;
public static void SetDialogs(UIElement element, ObservableCollection<MyWindow> value)
{
element.SetValue(DialogsPropertyKey, value);
}
public static ObservableCollection<MyWindow> GetDialogs(UIElement element)
{
var dialogs = (ObservableCollection<MyWindow>)element.GetValue(DialogsProperty);
if (dialogs == null)
{
dialogs = new ObservableCollection<MyWindow>();
SetDialogs(element, dialogs);
}
return dialogs;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您的代码实际上将在设计时执行,因此您可以简单地让它有条件地执行一些操作,以使设计人员尽可能不做任何令人不快的事情。要实现此目的,您需要能够以编程方式检测您正在设计器下运行,并且可以使用
DesignerProperties.IsInDesignModeProperty
来实现此目的,如下所述:Since your code will actually be executed at design time, you can simply have it conditionally do something that will make the designer not do anything unpleasant, to the extent that that is possible. To accomplish this you need to be able to detect programmatically that you are running under the designer and you can use
DesignerProperties.IsInDesignModeProperty
for that as described here:我决定将 MyWindow 的基类从 Window 更改为 ContentControl。对于我们的目的来说,这已经足够了。每个 ContentControl 在激活时都被包装到一个 Window 中。
I decided to change base class of MyWindow from Window to ContentControl. For our purposes it is sufficient. Each ContentControl is wrapped into a Window when becomes active.