1

I've seen a few different pages here on SO that discuss what I'm after, but I think my case is different enough to warrent a new question here. So I have a base class with no XAML.

public abstract class AbstractConfigWindow : System.Windows.Window
{
    public AbstractConfigWindow()
    {
        this.WindowState = System.Windows.WindowState.Maximized;
        this.WindowStyle = System.Windows.WindowStyle.None;

        this.SetResourceReference(System.Windows.Window.BackgroundProperty, "formBackground");
    }
    private bool locked = false;

    public bool Locked
    {
        get { return locked; }
        set { locked = value; }
    }
}

I have a 3 UserControls that I created. They have a background image of a computer, and 4 common buttons (unrelated to question, just details).

I have 2 Windows that extend the AbstractConfigWindow. One is for Landscape view and the other Portrait. (Not sure if this is the best way of doing it, but it works for me) I placed those 3 UserControls in each of the 2 Child Window classes. Since at one time both of those Windows were just simple Windows it was not a problem. Well now that I have this base class I want to put all the common things (such as the Window state, style background..etc, etc) in the base class. This would include the 3 user controls (which there will be more of those added as this program gets bigger) My first thought is to programmatically add new instances into a Grid.. and then just put that grid where I need to in each of the 2 windows. I hate how fragile it is though. This is a small example of what one of the 2 windows looks like.

<local:AbstractConfigWindow x:Class="UnitLibrary_WPF.ConfigureWindowLandscape"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:UnitLibrary_WPF"
        xmlns:b="clr-namespace:UnitLibrary_WPF.Buttons"
        xmlns:u="clr-namespace:UnitLibrary_WPF.UnitImagePanels"
        xmlns:s="clr-namespace:UnitLibrary_WPF.Storyboards"
        Width="800"
        Height="480">
    <local:AbstractConfigWindow.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </local:AbstractConfigWindow.Resources>
    <Viewbox Margin="10">
        <DockPanel x:Name="mainView"
                   Height="480"
                   Width="800">
            <local:uHeader DockPanel.Dock="Top" />
            <local:uBrightnessControl DockPanel.Dock="Top" Height="75" />
            <local:uOposButtons Margin="0,5,0,0" DockPanel.Dock="Right" />
            <Grid Margin="10">
                <Viewbox x:Name="u8800viewbox"
                         Visibility="Hidden">
                    <u:u8800Image />
                </Viewbox>
                <Viewbox x:Name="u9000viewbox"
                         Visibility="Hidden">
                    <u:u9000Image />
                </Viewbox>
                <Viewbox x:Name="u7200viewbox"
                         Visibility="Hidden">
                    <u:u7200Image>
                        <u:u7200Image.LayoutTransform>
                            <TransformGroup>
                                <ScaleTransform />
                                <SkewTransform />
                                <RotateTransform Angle="90" />
                                <TranslateTransform />
                            </TransformGroup>
                        </u:u7200Image.LayoutTransform>
                    </u:u7200Image>
                </Viewbox>
            </Grid>
        </DockPanel>
    </Viewbox>
</local:AbstractConfigWindow>

in the other window I do not rotate u7200Image (that is one of the 3 UserControls that I mentioned earlier).

So the question is how can I add those 3+ controls to the base class so I only have to add them to the base class and not each child class?

Robert Snyder
  • 2,399
  • 4
  • 33
  • 65
  • as a FYI this article is super close to what i want. http://stackoverflow.com/q/11140000/1329396 but I have to be able to move it all around. I think i made a mistake by creating a class file instead of a Window file... – Robert Snyder Aug 19 '13 at 17:05
  • Are you saying that the RotateTransform is the *only* difference between ConfigureWindowLandscape and ConfigureWindowPortrait? Why don't you create a property for that rotation? – Clemens Aug 19 '13 at 17:36
  • @Clemens There are some other asthetics that change. For instance u:POS Buttons goes from being docked to the right to being in an `Expander` and Dock Top. would like to add that to base class too, but one step at a time. Although your suggestion about a rotate property is a good idea beacuse the 4 buttons that reside in that one Usercontrol need to still face down instead of rotating with the control. – Robert Snyder Aug 19 '13 at 17:39
  • 1
    I would try to implement a flexible layout. Something that supports both configurations, depending on a single `Orientation` property. In your XAML you may bind layout related properties to the `Orientation` property by appropriate converters. – Clemens Aug 19 '13 at 17:43
  • @Clemens sounds complex, but well worth it if i could go back to just one window. – Robert Snyder Aug 19 '13 at 17:44

1 Answers1

1

Why don't you just create your 3 controls in a DataTemplate:

<DataTemplate DataType="{x:Type ViewModels:YourViewModel}">
    <!--Your 3 controls-->
</DataTemplate>

Then in each of your windows, you just need to add an instance of your data type and a ContentControland the 3 controls will magically appear.

<Window.Resources>
    <ViewModels:YourViewModel x:Key="InstanceOfYourDataType" />
</Window.Resources>

<ContentControl Content="{Binding Source={StaticResource InstanceOfYourDataType}}" />
Sheridan
  • 68,826
  • 24
  • 143
  • 183