【问题标题】:How could providing Parent Window as Pattern - WPF如何提供父窗口作为模式 - WPF
【发布时间】:2017-11-06 19:51:35
【问题描述】:

考虑我的项目有 4 个窗口,我尝试提供特定的关闭按钮和一个标题

我怎样才能使窗口对象和所有窗口都使用它作为模式。

这是我们为模式窗口提供的示例:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        WindowStyle="None" AllowsTransparency="True" >
<Grid>
<Button Content="Close" Height="40" VerticalAlignment="Top" HorizontalAlignment="Right"/>
<TextBlock VerticalAlignment="Top" HorizontalAlignment="Center" X:Name="WindowTitle/>
</Grid>
</Window>

如何将我的所有 Window 用作模式。谢谢

【问题讨论】:

  • 这是您要找的吗? stackoverflow.com/questions/7174315/…
  • @Aaron 没关系,谢谢,但是我应该如何在模式上有特定的按钮?
  • 您很可能会将基类按钮文本和功能绑定到基类中的一个(或多个)属性。然后,当您在子类中设置该属性时,“OnPropertyChanged”通知将对按钮执行它需要做的任何事情(添加不同的处理程序/绑定,删除不必要的处理程序/绑定,更改文本等)......或者,取决于想知道它有多疯狂,您可以像创建基本窗口一样创建一个“基本按钮”,然后完全投入其中。
  • 如果我理解正确的话。您可以在单独的 xaml 文件中定义资源并在其他窗口中使用它。查看以下链接中的“合并资源字典”部分:docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/…您需要定义资源并在您的窗口中重用它

标签: wpf window-style


【解决方案1】:

其实不用写父窗口。您可以改用StyleTemplate。更方便,被微软 WPF 团队推荐。

将下面的代码写入你的App.xaml,你会得到上面的图片:

<Application x:Class="Walterlv.Demo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             .........>
    <Application.Resources>
        <Style x:Key="Style.Window.Default" TargetType="Window">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Window">
                        <Grid Background="{TemplateBinding Background}">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="40"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <Button Grid.Row="0" Content="Close" Height="40" VerticalAlignment="Top" HorizontalAlignment="Right"/>
                            <TextBlock Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Center"
                                       Text="{TemplateBinding Title}"/>
                            <Border Grid.Row="1" BorderThickness="{TemplateBinding BorderThickness}"
                                    BorderBrush="{TemplateBinding BorderBrush}">
                                <!-- This is the container to host your Window.Content -->
                                <ContentPresenter/>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>
</Application>

而您只能使用一个属性Style 来共享这样的“模式”:

<Window x:Class="Walterlv.Demo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Style="{StaticResource Style.Window.Default}">

</Window>

您可以在App.xaml 文件中定义不同类型的样式,并在您的XxxWindow.xaml 中选择您需要的任何人。

【讨论】:

  • 太棒了,这正是我需要的,谢谢
猜你喜欢
  • 1970-01-01
  • 2012-06-14
  • 1970-01-01
  • 2014-11-03
  • 2015-05-29
  • 2010-09-30
  • 1970-01-01
  • 2011-10-19
  • 1970-01-01
相关资源
最近更新 更多