3

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.

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

Sagotharan
  • 2,586
  • 16
  • 73
  • 117

4 Answers4

3

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

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.


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.

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 />

example

Noctis
  • 11,507
  • 3
  • 43
  • 82
  • all windows means In my WPF project, I have used more the 25 WPF windows,. You already know that wpf has windows & pages na? – Sagotharan Nov 21 '13 at 06:01
  • 1
    :) ... you using 1 page and swapping it's content? you using different actual windows that open and close? that was the question ... in any case, the answer remains the same. Have 1 control that you define with header/content/footer. And then just swap the content, leaving the rest in place – Noctis Nov 21 '13 at 06:38
  • you using different actual windows that open and close? yes sir. Now I doing this way. Is this worst idea? How can make control with header-content-footer and how can I swap the content? – Sagotharan Nov 21 '13 at 07:32
  • Actually I want to changeable content sir, Now I am using ContentPresenter for that. I am not aware of MVVM models. Can you simplify this process. – Sagotharan Nov 22 '13 at 05:43
  • 1
    MVVM is a design patter that started with MS and goes nicely with WPF. It takes a bit of effort to learn, but once you get it, it's quite sweet and has plenty of benefits. Not much to do but read about it I guess... – Noctis Nov 22 '13 at 05:51
  • 1
    MVVM is definitely worth to learn, but this is purely View's responsibility and there is no need to bring ViewModels here. See my answer that demonstrates solution that I hope meets your requirements and is usable with or without mvvm. – Liero Jan 26 '15 at 10:40
2

Step 1: Define your window style with header and footer in app.xaml or in shared 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.

Style="{StaticResource HeaderFooterWindowStyle}"
Usman Zafar
  • 1,919
  • 1
  • 15
  • 11
  • I tried this one. Its Very Simple. I have one doubt how can i set the ContentPresenter condent in window? – Sagotharan Nov 22 '13 at 05:41
  • ContentPresenter here is used to show all your window`s controls. You don't need to set its content – Usman Zafar Nov 22 '13 at 05:45
  • oh! I want the footer textblock as marque, that i have do with animation. But I want to change the textblock text as per the MYSQL database. So i want to access this textblock I have named the textblock as footer1 but i can't get this in mainpage.xaml.cs page. How can i get this control access? – Sagotharan Nov 22 '13 at 06:12
  • 1
    You can get the Footer TextBlock by calling this.GetTemplateChild("F1") in OnApplyTemplate (where F1 is the TextBlock name). However accessing controls in this way is not recommended. If you just want to change its text, bind Text property to something. – Usman Zafar Nov 22 '13 at 06:57
  • @UsmanZafar is there a way to do this in .net maui? I tried it and there is no Template property. – tbear Dec 03 '22 at 16:59
  • I made a question here: https://stackoverflow.com/questions/74668855/header-and-footer-in-net-maui – tbear Dec 03 '22 at 17:10
0

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

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.

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

Mario Vernari
  • 6,649
  • 1
  • 32
  • 44
  • I may understand that my answer is not the best one, but...why should be downvoted? – Mario Vernari Nov 21 '13 at 12:48
  • here, have an upvote mate :) i think this is actually better solution if you want to have ALL the windows with same header and footer ... as you said, derive from Window, and simply add items in this base class on a proper event, then all new xaml windows will be of this type ... nice solution [though your like does not work ] – frno Nov 21 '13 at 22:39
  • If you want to just add some visual stuff to any control, you should edit its Template. you can use styles for this. Create new controls only when you need to add some properties or funcionality. Why would you create MyWindows class, change nothing in code and then define xaml template, when you could just create new style for windows and edit xaml template. – Liero Jan 23 '15 at 08:37
  • @Liero, there are DataTemplates, ControlTemplates and Styles: each one has its own purpose. The DataT relies on data, so that's not the case. ControlT can be used, but it's valid for any instance and not easy to modify. Styles are, IMHO, the very final refinement, such colors, fonts, etc. A derivation of a Window, instead, could make your life way easier. – Mario Vernari Jan 23 '15 at 12:39
  • @Mario: I suppose you want to define Header and footer in ContentProperty of MyWindow. How would you define let's say MainWindow's or any other Window's content itsetf you change it to inherit from MyWindow instead of Window? Footer and Header will be overwritten if you write any xaml into MainWindow or place anything onto designer surface in Visual Studio. – Liero Jan 26 '15 at 09:36
  • Not clear what you mean, but right, I'd add H and F to mimic kinda "HeaderedFooteredContentControl". My habit is with MVVM in mind, and styles are just a refinement. Of course, you can use WPF as it were a new-Winforms platform... – Mario Vernari Jan 26 '15 at 09:42
0

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.

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

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.

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

Liero
  • 25,216
  • 29
  • 151
  • 297