【发布时间】:2019-12-13 16:44:27
【问题描述】:
我已经为这样的对话框创建了自定义窗口类:
public class DialogBase : Window
{
// common stuff here
}
public class DialogOkCancel : DialogBase
{
static DialogOkCancel()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DialogOkCancel),
new FrameworkPropertyMetadata(typeof(DialogOkCancel)));
}
// specific stuff here
}
样式看起来像这样,并且包含特定于对话框的内容,例如标题栏、确定/取消按钮等。
<Style x:Key="DialogBaseStyle" TargetType="{x:Type base:DialogBase}" BasedOn="{StaticResource {x:Type Window}}">
<Setter Property="ShowInTaskbar" Value="False"/>
<!-- more stuff here -->
</Style>
<Style TargetType="{x:Type base:DialogOkCancel}" BasedOn="{StaticResource DialogBaseStyle}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type base:DialogOkCancel}">
<!-- more stuff here -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这在运行时工作得非常好。此外,对话框在 XAML 设计器中显示良好。
但是,在 XAML 设计器中,DialogOkCancel 样式也会应用于其他所有其他窗口,例如应用程序主窗口(它只是从普通的 Window 派生的)。标题栏、按钮和一切。我真的不明白这一点,因为TargetType 是特定的。这不会在运行时发生。
我在这里没有看到什么?
【问题讨论】:
-
可能是 XAML 设计器中的错误...
-
这件事一直发生在我身上,我的解决方案是我只是把键放在样式上并将其添加到特定的窗口。由于某些奇怪的原因,当您拥有“BasedOn”属性并且它指向某些基本控件时,XAML 设计器会很困惑!
-
@J.Memisevic 我希望避免基于键的样式以避免重复。但是,是的,我认为这是最后的手段。