gpt4 book ai didi

c# - 如何在wpf应用程序中设置所有窗口的背景图片

转载 作者:行者123 更新时间:2023-11-30 15:21:51 25 4
gpt4 key购买 nike

我想在一个地方设置我的 wpf 应用程序所有窗口中所有窗口的背景图像和所有数据网格的样式

我的 app.xaml 文件中有以下代码

<Application.Resources>

<!--Styles for woindow-->
<ImageBrush x:Key="WindowBackgroungImage" ImageSource="Icons\clinic.jpg"/>
<LinearGradientBrush x:Key="WindowBorderBrush" EndPoint="0.5,1" StartPoint="0.5,0" >
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>

<!--Styles for Data Grid-->
<Style x:Key="DGCHeaderStyle" TargetType="DataGridColumnHeader">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontSize" Value="14" />
<Setter Property="FontFamily" Value="Calibri" />
<Setter Property="FontWeight" Value="SemiBold" />
<!--<Setter Property="HorizontalAlignment" Value="Center" />-->
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>

<LinearGradientBrush x:Key="DataGridRowBackground" EndPoint="195.5,455" StartPoint="195.5,0" MappingMode="Absolute" SpreadMethod="Repeat">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FF0FC9E6" Offset="1"/>
<GradientStop Color="#FF6CA3AC"/>
</LinearGradientBrush>
<RadialGradientBrush x:Key="DataGridAlternatingRowBackground" SpreadMethod="Reflect" Center="195.5,227.5" GradientOrigin="195.5,227.5" MappingMode="Absolute" RadiusY="227.5" RadiusX="195.5">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FFEEA21F" Offset="1"/>
<GradientStop Color="#FFF7F3E9"/>
</RadialGradientBrush>

<Style TargetType="DataGrid">
<Setter Property="ColumnHeaderStyle" Value="{DynamicResource DGCHeaderStyle}"/>
<Setter Property="Background" Value="{x:Null}" />
<Setter Property="HorizontalGridLinesBrush" Value="Black"/>
<Setter Property="VerticalGridLinesBrush" Value="Black"/>
<Setter Property="RowBackground" Value="{StaticResource DataGridRowBackground}"/>
<Setter Property="AlternatingRowBackground" Value="{StaticResource DataGridAlternatingRowBackground}" />
</Style>

<!--Settin window style-->
<Style TargetType="Window">
<Setter Property="BorderBrush" Value="{StaticResource WindowBorderBrush}"/>
<Setter Property="Background" Value="{StaticResource WindowBackgroungImage}"/>
</Style>

</Application.Resources>

DataGrid 的样式在设计器中按需应用,在我开始调试时也是如此背景图像根据需要出现在 visual studio 的设计器中,但当我在 Debug模式下启动应用程序时,它没有显示在任何窗口中。

当我在“mainwindow.xaml”文件中添加以下行时,背景图像会按需要显示

<Window.Background>
<ImageBrush ImageSource="Icons\clinic.jpg"/>
</Window.Background>

我的项目中有一个图标文件夹,其中放置了名为 clinic.jpg 的背景图像

最佳答案

这不是您的 ImageBrush 的问题,而是您不能在此处使用基类型 Window 设置默认样式的任何属性。原因是您的 Window 类型为 MainWindow 而不是 Window。因此它不起作用。如果您提供窗口类型,您将能够为该窗口设置它。

<Application.Resources>
<Style TargetType="{x:Type local:MainWindow}">
<Setter Property="BorderBrush" Value="{StaticResource WindowBorderBrush}"/>
<Setter Property="Background" Value="{StaticResource WindowBackgroungImage}"/>
</Style>
</Application.Resources>

所以基本上您必须为所有窗口类型定义样式。您可以定义一种样式,也可以使用 BasedOn

简单地创建其他样式
    <Style TargetType="Window">
<Setter Property="BorderBrush" Value="{StaticResource WindowBorderBrush}"/>
<Setter Property="Background" Value="{StaticResource WindowBackgroungImage}"/>
</Style>

<Style TargetType="{x:Type local:MainWindow}"
BasedOn="{StaticResource {x:Type Window}}"/>

关于c# - 如何在wpf应用程序中设置所有窗口的背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36679108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com