gpt4 book ai didi

wpf - 尝试重用 WPF 窗口

转载 作者:行者123 更新时间:2023-12-02 22:18:56 25 4
gpt4 key购买 nike

下面的 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 的属性 setter
  2. 我不知道如何使 InputBindings 成为样式的一部分

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

干杯。
贝里尔

最佳答案

为什么不简单地创建一个没有内容的此类窗口,然后在显示之前添加您选择的 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();

关于wpf - 尝试重用 WPF 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7385381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com