The xaml below is for a Window I am using in several presentations where the only thing that varies is the UserControl that it hosts:
<Window x:Class="Smack.ConstructionAdmin.Presentation.Wpf.Views.Admin.Employees.EmployeeShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Smack.ConstructionAdmin.Presentation.Wpf.Views.Admin.Employees"
xmlns:s="clr-namespace:Smack.ConstructionAdmin.Presentation.Wpf"
xmlns:cmdRef="clr-namespace:Smack.Core.Presentation.Wpf.ViewModels.Commands.Reference;assembly=Smack.Core.Presentation.Wpf"
Background="{DynamicResource WaveWindowBackground}"
Title="{Binding Source={x:Static s:Strings.AppName}}"
Icon="pack://application:,,,/Smack.ConstructionAdmin.Presentation.Wpf;component/Images/Time-Machine_16.png"
FontFamily="Arial"
WindowStartupLocation="CenterScreen" Width="750" Height="600"
>
<DockPanel>
<local:EmployeeShellUserControl DataContext="{Binding}" />
</DockPanel>
<Window.InputBindings>
<cmdRef:KeyBindingEx CommandReference="{Binding AddCommand}"/>
<cmdRef:KeyBindingEx CommandReference="{Binding EditCommand}"/>
<cmdRef:KeyBindingEx CommandReference="{Binding DeleteCommand}"/>
</Window.InputBindings>
</Window>
So it seems to make sense to reuse the parts that do not vary somehow. Here is my first attempt at doing so with a style:
<Style x:Key="MyWindowStyle" TargetType="{x:Type Window}">
<Setter Property="Background" Value="{DynamicResource WaveWindowBackground}"></Setter>
<Setter Property="FontFamily" Value="Arial"></Setter>
<Setter Property="Height" Value="600"></Setter>
<Setter Property="Width" Value="750"></Setter>
<Setter Property="Title" Value="{Binding AppName}"></Setter>
<Setter Property="Icon" Value="{Binding IconUri}"></Setter>
</Style>
Pain points
- I couldn’t find a property setter for WindowStartupLocation
- I don’t see how to make the InputBindings part of the style
IS using a style the right approach or is there some other technique I need to use? How can I set the above properties?
Cheers.
Berryl
Why don’t you simply create a window of this type without content, and then add the
UserControlof your choice as itsContentbefore showing it? You won’t need multipleWindowsubclasses, and you won’t need to mess with styles.A trivial example, where we ‘re setting the window’s content to a string (normally you ‘d use some appropriate
UserControl):If you want to insert a complex
UserControl, say this one:You would do: