Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF表单继承问题_Wpf - Fatal编程技术网

WPF表单继承问题

WPF表单继承问题,wpf,Wpf,我对WPF中的表单继承问题有一些疑问。 我已经读到WPF表单中没有视觉继承。 我想将我的项目作为一个基本表单来编写,其他表单将从中继承 一些可能的解决方案是使用UserControl,并在子表单中使用它。 问题是,我必须在继承基本形式的每个新形式中一次又一次地定义它 我可以用另一种方式实现它,而不在子表单中定义它吗?您可以使用一种样式来处理这个问题,而标记要比用户控件少(您只需在每个窗口元素中设置style=“{StaticResource myWindowStyle}”)。使用这种样式,您可以

我对WPF中的表单继承问题有一些疑问。 我已经读到WPF表单中没有视觉继承。 我想将我的项目作为一个基本表单来编写,其他表单将从中继承

一些可能的解决方案是使用
UserControl
,并在子表单中使用它。 问题是,我必须在继承基本形式的每个新形式中一次又一次地定义它


我可以用另一种方式实现它,而不在子表单中定义它吗?

您可以使用一种样式来处理这个问题,而标记要比用户控件少(您只需在每个窗口元素中设置
style=“{StaticResource myWindowStyle}”
)。使用这种样式,您可以覆盖窗口的
模板
,在窗口内容周围放置您喜欢的任何chrome。使用
Content=“{TemplateBinding Content}”
添加一个
ContentControl
,无论您希望在何处显示sepcific窗口的内容

不幸的是,您不能轻松地将其应用于所有窗口,因为设置通用样式将仅应用于基本
窗口
控件,并且应用程序的每个窗口都将是从
窗口
派生的类型

编辑:如果要在窗口中指定模板控件,可以对样式执行以下操作:

<Style x:Key="MyWindowStyle" TargetType="Window">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Window">
        <StackPanel>
          <Button>This button will appear on every window</Button>
          <ContentControl Content="{TemplateBinding Content}" />
        </StackPanel>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

此按钮将出现在每个窗口上

您确实意识到WPF使用的是windows,而不是表单,对吗?请参阅本文。为什么不定义一个BaseWindow,然后创建“son UserControls”,而不是创建“son表单”。当您需要显示特定的“son UserControl”时,请执行:
var sonWindow=new BaseWindow{Content=new SonUserControl()}这是一个选项,但我不能在设计时看到它。我会把它作为一个选项。谢谢..嗨,我知道我能运用风格。但在我看来,这一切都与窗户的“风格”有关。我真的无法定义这些控件。我错了吗?正如我所说,您可以覆盖模板来添加控件。有关示例,请参见我的编辑。