开发者

trying to reuse a WPF window

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>
  开发者_C百科  <Setter Property="Width" Value="750"></Setter>
    <Setter Property="Title" Value="{Binding AppName}"></Setter>
    <Setter Property="Icon" Value="{Binding IconUri}"></Setter>
</Style>

Pain points

  1. I couldn't find a property setter for WindowStartupLocation
  2. 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 UserControl of your choice as its Content before showing it? You won't need multiple Window subclasses, 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):

var window = new EmployeeShellView();
window.Content = "Hello world!"; // set to your UserControl
window.Show();

If you want to insert a complex UserControl, say this one:

<UserControl x:Class="MyControl">
    <DockPanel>
        <local:EmployeeShellUserControl DataContext="{Binding}"  />
    </DockPanel>
</UserControl>

You would do:

var window = new EmployeeShellView();
window.Content = new MyControl();
window.Show();


I suggest to overcome your problems with attached behavior that will be set on the style.

Just Google attached behavior wpf

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜