尝试重用WPF窗口
下面的xaml用于我在几个演示文稿中使用的窗口,其中唯一不同的是它所承载的UserControl:尝试重用WPF窗口,wpf,window,styles,Wpf,Window,Styles,下面的xaml用于我在几个演示文稿中使用的窗口,其中唯一不同的是它所承载的UserControl: <Window x:Class="Smack.ConstructionAdmin.Presentation.Wpf.Views.Admin.Employees.EmployeeShellView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="htt
<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>
痛点
Berryl为什么不创建一个没有内容的此类窗口,然后在显示之前添加您选择的
UserControl
作为其内容?您不需要多个Window
子类,也不需要弄乱样式
一个简单的例子,我们将窗口的内容设置为字符串(通常您会使用一些适当的UserControl
):
如果要插入复杂的用户控件
,请说:
<UserControl x:Class="MyControl">
<DockPanel>
<local:EmployeeShellUserControl DataContext="{Binding}" />
</DockPanel>
</UserControl>
我建议克服你的问题与附加行为,将设置的风格
只需谷歌附加行为wpf听起来很完美!但不确定你的意思-你能划出一些代码和/或xaml来更好地说明吗?@Berryl:我已经添加了一个示例,它看起来还好吗?所以让ShellView保持原样,实例化它并设置内容?我替换现有UserControl的部分是如何成为内容的,或者这是Content属性的定义?我在窗口的文档中看不到内容。这是一个标准的财产,我错过了它还是一个地方持有人,你正在添加?没有得到那个角色yet@Berryl:它是--窗口
源于内容控件
。
<UserControl x:Class="MyControl">
<DockPanel>
<local:EmployeeShellUserControl DataContext="{Binding}" />
</DockPanel>
</UserControl>
var window = new EmployeeShellView();
window.Content = new MyControl();
window.Show();