Aud*_*dio 5 c# wpf xaml window
我正在VS2013中创建一个简单的WPF项目,我想将属性应用于主窗口。我将它们设置在我的App.xaml文件中,如下所示:
<Application.Resources>
<Style TargetType="Window">
<Setter Property="Background" Value="#FF2D2D30" />
</Style>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
问题是什么也没发生。TargetType但是,当我将其更改为Grid时,setter属性可以正常工作。为什么会这样?
有必要在以下位置添加构造Window:
Style="{StaticResource {x:Type Window}}"
Run Code Online (Sandbox Code Playgroud)
Window 在 XAML 中:
<Window x:Class="WindowStyleHelp.MainWindow"
Style="{StaticResource {x:Type Window}}"
...>
Run Code Online (Sandbox Code Playgroud)
或者Style像这样在资源中定义:
xmlns:local="clr-namespace:MyWpfApplication"
<Application.Resources>
<Style TargetType="{x:Type local:MainWindow}">
<Setter Property="Background" Value="#FF2D2D30"/>
</Style>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
回答这个问题“为什么它不起作用”。
Target 类型未应用于您的 Window 的原因是,您使用的是名称为“MainWindow”的窗口的派生类型。因此,在您的样式资源中,您必须将目标类型设置为派生类型 (MainWindow)。通过这样做,它将仅应用于“MainWindow”窗口。
<Style TargetType="local:MainWindow">
<Setter Property="Background" Value="#FF2D2D30" />
</Style>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2650 次 |
| 最近记录: |