尝试重用 WPF 窗口

发布于 2024-12-04 06:06:53 字数 2214 浏览 1 评论 0原文

下面的 xaml 适用于我在多个演示文稿中使用的窗口,其中唯一变化的是它托管的 UserControl:

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

因此,重用不以某种方式变化的部分似乎是有意义的。这是我第一次尝试使用样式这样做:

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

痛点

  1. 我找不到 WindowStartupLocation 的属性设置器
  2. 我不知道如何使 InputBindings 成为样式的一部分

是使用样式是正确的方法还是还有其他方法我需要使用的技术?如何设置以上属性?

干杯。
贝里尔

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

  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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灯下孤影 2024-12-11 06:06:53

为什么不简单地创建一个没有内容的此类窗口,然后在显示之前添加您选择的 UserControl 作为其 Content 呢?您不需要多个 Window 子类,也不需要搞乱样式。

一个简单的例子,我们将窗口的内容设置为字符串(通常您会使用一些适当的UserControl):

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

如果您想插入一个复杂的UserControl,比如说这个:

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

你会这样做:

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

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();
冬天旳寂寞 2024-12-11 06:06:53

我建议通过将在样式上设置的附加行为来克服您的问题。

只是 Google 附加行为 wpf

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

Just Google attached behavior wpf

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文