所有 WPF 窗口中的页眉和页脚相同

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20113405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:05:07  来源:igfitidea点击:

Same header & footer in all WPF windows

wpfwindow

提问by Sagotharan

In my project I have same header & footer for all windows (I have more then 20 wpf windows in this project), Only Content has changed. This is WPF project. If its a asp.net I have used master page in that.

在我的项目中,所有窗口都有相同的页眉和页脚(我在这个项目中有超过 20 个 wpf 窗口),只有内容发生了变化。这是 WPF 项目。如果它是一个 asp.net,我在其中使用了母版页。

Now I copy paste header & footer all the windows. If any small changes needed in Header, I have force to do in all windows header. Is any solutions there?

现在我复制粘贴页眉和页脚所有窗口。如果标题中需要任何小的更改,我必须在所有窗口标题中进行。有什么解决办法吗?

enter image description here

在此处输入图片说明

采纳答案by Noctis

I'm not a 100% sure what do you mean by "all windows", but a good approach would be to have a headerand a footerUserControls.

我不是 100% 确定您所说的“所有窗口”是什么意思,但一个好的方法是拥有一个header和一个footerUserControls。

You can set them whichever way you want, (text, colors, etc), and then, in all your windows you could have a grid with 3 elements, stackpanel, dockpanel, whatever you want, which will have the header, then your content, and last, your footer.

您可以按您想要的任何方式设置它们(文本、颜色等),然后,在您的所有窗口中,您都可以拥有一个包含 3 个元素的网格,堆栈面板、停靠面板,无论您想要什么,其中都有header,然后是您的content,和最后,你的footer.



Edit:As per comment: Instead of having this:

编辑:根据评论:而不是有这个:

<Window ...>
    <StackPanel>
        <Label>one</Label>
        <Label>two </Label>
        <Label>three</Label>
    </StackPanel>
</Window>

You can have a UserControls (lets say called HeaderControl), and then all you need to do is 1: Add the namespace so you can use it.
2. Add it like you'll add any other control. 3. Enjoy reusability.

您可以拥有一个 UserControls(可以说称为HeaderControl),然后您需要做的就是 1:添加命名空间以便您可以使用它。
2. 像添加任何其他控件一样添加它。3. 享受可重用性。

steps explanation:

步骤说明:

  1. Add this to your windown with the rest of the xmlns....definitions:
    xmlns:controls="clr-namespace:WpfApplication1"

  2. Changing the first control from label to our resource:
    Instead of <Label>one</Label>write: <controls:HeaderControl />

  1. 将此与其余定义一起添加到您的 windown 中xmlns....
    xmlns:controls="clr-namespace:WpfApplication1"

  2. 将第一个控件从标签更改为我们的资源:
    而不是<Label>one</Label>写入:<controls:HeaderControl />

example

例子

回答by Usman Zafar

Step 1:Define your window style with header and footer in app.xaml or in shared ResourceDictionary.

第 1 步:在 app.xaml 或共享 ResourceDictionary 中定义带有页眉和页脚的窗口样式。

<Style x:Key="HeaderFooterWindowStyle" TargetType="Window">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Window}">
                <Grid Background="{TemplateBinding Background}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <!-- Header -->
                    <Border Grid.Row="0" Background="Red">
                        <TextBlock Text="Header"/>
                    </Border>

                    <!-- Body -->
                    <ContentPresenter Grid.Row="1"/>

                    <!-- Footer -->
                    <Border Grid.Row="2" Background="Red">
                        <TextBlock Text="Footer"/>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Step 2:Set Style to windows.

第 2 步:将样式设置为 windows。

Style="{StaticResource HeaderFooterWindowStyle}"

回答by Liero

I suppose you need to specify Footer and Header in each window, but you want to have consistent look & feel.

我想您需要在每个窗口中指定页脚和页眉,但您希望具有一致的外观和感觉。

In general, if you want to add some reusable visual stuff around content, you should use content control and edit its template.

一般来说,如果您想在内容周围添加一些可重用的视觉内容,您应该使用内容控件并编辑其模板。

If you need to specify also header for the content, you should use HeaderedContentControl and edit it's template.

如果您还需要为内容指定标题,您应该使用 HeaderedContentControl 并编辑它的模板。

If you need to specify also footer, just create you own control inherited from HeaderedContentControl and specify Footer property.

如果您还需要指定页脚,只需创建您自己的从 HeaderedContentControl 继承的控件并指定页脚属性。

here is expampe of usage:

这是用法说明:

<controls:HeaderFooterContentControl 
    Header="Some simple header" 
    Footer="There could be xaml footer as well!>
    <Grid>
        <!--Place your content here-->
    </Grid>
</controls:HeaderFooterContentControl>

and implementation:

和实施:

public class HeaderFooterContentControl : HeaderedContentControl
{
    public object Footer
    {
        get { return (object) GetValue(FooterProperty); }
        set { SetValue(FooterProperty, value); }
    }

    public static readonly DependencyProperty FooterProperty = DependencyProperty.Register("Footer", typeof (object), typeof (HeaderFooterContentControl));
}

and the template:

和模板:

<Style TargetType="controls:HeaderFooterContentControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="controls:HeaderFooterContentControl">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <Border Grid.Row="0" Background="White">
                        <ContentControl Content="{TemplateBinding Header}"  Foreground="Red"
                                        ContentTemplate=""   />
                    </Border>

                    <Border Grid.Row="1" Background="Red">
                        <ContentControl Content="{TemplateBinding Content}" Foreground="White"
                                        ContentTemplate="{TemplateBinding ContentTemplate}"
                                        ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"/>
                    </Border>

                    <Border Grid.Row="2" Background="White">
                        <ContentControl Content="{TemplateBinding Footer}" Foreground="Red"  />
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Notice, that this have nothing to do with MVVM, but since Header and Footer are dependency properties, it could be easily used in any MVVM scenario.

请注意,这与 MVVM 无关,但由于 Header 和 Footer 是依赖属性,因此可以轻松地在任何 MVVM 场景中使用。

I would definitely avoid binding to viewmodel properties in ControlTemplate if possible.

如果可能,我肯定会避免绑定到 ControlTemplate 中的视图模型属性。

回答by Mario Vernari

Why not creating your own "MyWindow" class deriving from a regular Window?

为什么不创建自己的从常规窗口派生的“MyWindow”类?

It's a fair common practice whereas you need to override/customize the standard Window component. After you have the new component, you should write a custom template so that any Content will be displayed in your desired fashion.

这是一种常见的做法,而您需要覆盖/自定义标准 Window 组件。有了新组件后,您应该编写一个自定义模板,以便任何内容都以您想要的方式显示。

Have a look here: http://msdn.microsoft.com/en-us/library/aa969824(v=vs.110).aspx

看看这里:http: //msdn.microsoft.com/en-us/library/aa969824(v=vs.110).aspx