2011-04-28 36 views
0

我已经创建了窗口派生类(WindowAttachedCollection.MyWindow)和持有这些窗口的集合的附加属性。但在VS 2010尝试WPF设计器创建WindowInstance对象为集合中的每个窗口,它抛出ArgumentException的:如何禁用WPF设计器中的自定义窗口呈现?

值“Microsoft.Expression.Platform.WPF.InstanceBuilders.WindowInstance”的类型是不是“WindowAttachedCollection.MyWindow”并且不能用于这个通用集合。 参数名称:值

因此,它打破了WPF设计师。

是否有任何方式如何禁用实例WindowInstance而不是WPF设计器中的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; 
} 

回答

0

我决定改变基类为mywindow的从Window到ContentControl。就我们的目的而言,这足够了。每个ContentControl在变为活动状态时都被封装到一个窗口中。

1

因为你的代码实际上在设计时被执行,你可以简单地把它有条件地做一些事情,这将使设计师不需要做任何事情不愉快,到这是可能的程度。要做到这一点,你需要能够检测到编程,你是设计师下运行,你可以使用DesignerProperties.IsInDesignModeProperty为如下所述:

+0

感谢您的回复,但我该如何使用它?我试图给附加属性的getter和setter一些条件,但也抛出异常。理想的方式是设计师忽略这个附属性。 – Kolyk 2011-04-28 08:41:27

+0

@Kolyk见http://stackoverflow.com/questions/826636/viewing-xaml-file-in-the-visualstudio-ui-designer-executes-the-code-behind-file – DuckMaestro 2012-05-03 22:43:53

+0

感谢您的帮助。但它并不需要。 – Kolyk 2012-05-18 08:47:28