WPF表单继承问题

时间:2012-03-07 17:22:11

标签: wpf

我对WPF中的表单继承问题有一些疑问。 我已经读过WPF Forms中没有可视继承。 我想将我的项目编写为基本表单,其他表单继承自它。

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

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

1 个答案:

答案 0 :(得分:0)

您可以使用样式来处理 less 标记而不是UserControl(您只需在每个Window元素中设置Style="{StaticResource myWindowStyle}")。使用该样式,您可以覆盖窗口的Template,在窗口内容周围放置您喜欢的任何镶边。在ContentControl添加Content="{TemplateBinding Content}",只要您想要显示特定窗口的内容,就可以添加Window

遗憾的是,您无法轻松地将此应用于所有Windows,因为设置通用样式仅适用于基本Window控件,并且每个应用程序的窗口都将是源自{{1 }}

编辑:如果您想在窗口中指定模板化控件,可以使用您的样式执行以下操作:

<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>
相关问题