问题描述
WPF 有没有推荐的方法来创建一个通用的窗口样式以在整个应用程序中使用?我的应用程序中出现了几个对话框,我希望它们的样式都相同(相同的窗口边框、确定/取消按钮位置等),并且每个对话框都有不同的内容",具体取决于具体情况.因此,一个对话框中可能有一个列表框,一个可能有一个文本框,等等.
Is there any recommended way with WPF to create a common window style to be used across an application? I have several dialogs that appear in my app, and I would like them all to be styled the same (same window border, ok/cancel button position, etc) and simply have different 'content' in each, depending on the situation. So, one dialog might have a list box in it, one might have a textbox, and so on.
我了解如何制作基本的 .cs 用户控制文件,但我终其一生都无法找到一个好方法来创建一个在启动时可以承载不同内容的单一窗口?
I understand how to make base .cs usercontrol files, but I can't for the life of me work out a good way to create a single window which can host different content when launched?
干杯,rJ
推荐答案
一种方法是创建一个新的自定义控件,我们称之为 DialogShell
:
One way to do it would be a new custom control, let's call it DialogShell
:
namespace Test.Dialogs
{
public class DialogShell : Window
{
static DialogShell()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DialogShell), new FrameworkPropertyMetadata(typeof(DialogShell)));
}
}
}
现在需要一个通常在Themes/Generic.xaml
中定义的模板,在那里你可以创建默认结构并绑定Content
:
This now needs a template which would normally be defined in Themes/Generic.xaml
, there you can create the default structure and bind the Content
:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Test.Dialogs">
<Style TargetType="{x:Type local:DialogShell}" BasedOn="{StaticResource {x:Type Window}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:DialogShell}">
<Grid Background="{TemplateBinding Background}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- This ContentPresenter automatically binds to the Content of the Window -->
<ContentPresenter />
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="5" HorizontalAlignment="Right">
<Button Width="100" Content="OK" IsDefault="True" />
<Button Width="100" Content="Cancel" IsCancel="True" />
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
这只是一个示例,您可能希望将这些按钮与需要在 cs 文件中定义的自定义事件和属性挂钩.
This is just an example, you probably want to hook up those buttons with custom events and properties you need to define in the cs-file.
这个shell可以这样使用:
This shell then can be used like this:
<diag:DialogShell x:Class="Test.Dialogs.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:diag="clr-namespace:Test.Dialogs"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBlock Text="Lorem Ipsum" />
</Grid>
</diag:DialogShell>
namespace Test.Dialogs
{
public partial class Window1 : DialogShell
{
public Window1()
{
InitializeComponent();
}
}
}
事件连接示例(但不确定这是否是正确"的方法)
Event wiring example (not sure if this is the "correct" approach though)
<Button Name="PART_OKButton" Width="100" Content="OK" IsDefault="True" />
<Button Name="PART_CancelButton" Width="100" Content="Cancel" IsCancel="True" />
namespace Test.Dialogs
{
[TemplatePart(Name = "PART_OKButton", Type = typeof(Button))]
[TemplatePart(Name = "PART_CancelButton", Type = typeof(Button))]
public class DialogShell : Window
{
//...
public DialogShell()
{
Loaded += (_, __) =>
{
var okButton = (Button)Template.FindName("PART_OKButton", this);
var cancelButton = (Button)Template.FindName("PART_CancelButton", this);
okButton.Click += (s, e) => DialogResult = true;
cancelButton.Click += (s, e) => DialogResult = false;
};
}
}
}
这篇关于如何创建通用的 WPF 基础窗口样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!