WPF,为何样式对TargetType=“Window“无效

在wpf中,您直接定义样式,并设置TargetType="Window",会发现是无效的。要怎么解决呢?

让我们定义一个样式文件,路径为Styles/App.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:Zs.PlayAssists.Styles">
    <Style TargetType="Window">
        <Setter Property="Height" Value="450"></Setter>
        <Setter Property="Width" Value="800"></Setter>
    </Style>
</ResourceDictionary>

然后在入口文件App.xaml中添加以下代码:

<Application x:Class="Zs.PlayAssists.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Zs.PlayAssists"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Styles/App.xaml"></ResourceDictionary>
                <ResourceDictionary Source="/Styles/Control.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

于是我们在入口的主应用程序文件App.xaml文件中引入了样式文件App.xaml文件。请注意,这两个文件虽然名字一样,但文件不同。一个是应用程序入口文件,一个是样式文件。如下图所示:

运行程序,您会发现,您写的高度Height=450,宽度=800对窗体MainWindow是不生效的。

那么要怎么做呢?

方法一,给Window文件添加构造函数:

Style="{StaticResource {x:Type Window}}"

方法二,在资源文件中指定定义:

TargetType="{x:Type local:MainWindow}"

 

祝您用餐愉快。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 如下是一个标准的 ControlTemplate,它的 TargetType 为 ContentControl,在 WPF 中使用: ```xml <ControlTemplate TargetType="{x:Type ContentControl}"> <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"> <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/> </Border> </ControlTemplate> ``` 这个 ControlTemplate 定义了一个 Border 元素,它的背景、边框画刷和边框粗细都是由 ContentControl 绑定的。它的子元素是 ContentPresenter,它的内容、内容模板都是由 ContentControl 绑定的。 ### 回答2: 一个符合标准的WPF中的ControlTemplate,其TargetType为ContentControl,可以被如下定义和实现: ```xml <Style TargetType="{x:Type ContentControl}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ContentControl}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Content="{TemplateBinding Content}" /> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> ``` 这个ControlTemplate的目标类型是ContentControl,使用了一个Border作为容器,用于设置边框背景和边框样式。内部有一个ContentPresenter,用于呈现ContentControl的内容。ContentPresenter的水平对齐和垂直对齐方式都设置为居中,以确保内容在容器中居中显示。 在实际使用时,可以将此ControlTemplate应用于任何TargetType为ContentControl的控件,例如: ```xml <ContentControl Template="{StaticResource StandardContentControlTemplate}"> <!-- 这里放置需要呈现的内容 --> </ContentControl> ``` 通过这个标准的ControlTemplate,可以统一ContentControl的外观样式,增强应用界面的一致性和美观性。 ### 回答3: 当我们在WPF中使用ControlTemplate时,"TargetType="{x:Type ContentControl}"定义的模板将适用于ContentControl及其所有派生类的控件。 以下是一个WPF中标准的,TargetType="{x:Type ContentControl}"的控件模板的示例: ```xml <ControlTemplate TargetType="{x:Type ContentControl}"> <Border BorderBrush="Black" BorderThickness="1" Background="LightGray" CornerRadius="5"> <!-- 内容控件的内容将放置在这里 --> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> </Border> </ControlTemplate> ``` 在这个示例中,我们使用了一个Border控件来包裹ContentControl的内容。该Border具有黑色边框、灰色背景和圆角边框。 ContentPresenter用于呈现ContentControl中的内容,它被设置为水平和垂直居中,使得内容在Border中居中显示。 使用这个模板时,可以通过将模板应用到ContentControl的Style中来覆盖默认模板。例如: ```xml <ContentControl Content="Hello, World!" Style="{StaticResource MyContentControlStyle}" /> ``` 其中,MyContentControlStyle是已定义的样式,它引用了上述的标准模板。 这样,ContentControl将使用定义的控件模板进行呈现,显示带有边框和背景的内容。 希望这个回答能够对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值