下面的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>
使用正确方法的风格还是我需要使用其他技术?如何设置以上属性?
干杯。
Berryl
答案 0 :(得分:4)
为什么不在没有内容的情况下创建此类型的窗口,然后在显示之前添加您选择的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();
答案 1 :(得分:0)
我建议你克服你在风格上设置的附加行为的问题。
Google附加行为wpf