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

C# 如何在wpf应用程序中设置所有窗口的背景图像,c#,wpf,visual-studio,xaml,C#,Wpf,Visual Studio,Xaml,我想在一个地方设置wpf应用程序所有窗口中所有窗口的背景图像和所有数据网格的样式 我的app.xaml文件中有以下代码 <Application.Resources> <!--Styles for woindow--> <ImageBrush x:Key="WindowBackgroungImage" ImageSource="Icons\clinic.jpg"/> <LinearGradientBrush x:Key="Win

我想在一个地方设置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的样式在designer中以及在我开始调试时根据需要应用 背景图像会根据需要显示在visual studio的designer中,但在调试模式下启动应用程序时,它不会显示在任何窗口中

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

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


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

这不是ImageBrush的问题,而是您不能在此处使用基本类型
窗口设置任何默认属性样式。原因是您的窗口的类型是
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,但我希望能够使用单个样式设置所有窗口的背景图像您不能为所有自定义窗口使用单个默认样式。您必须创建不同的样式,如我在回答中所解释的,或者使用
x:Key
创建一个样式,并通过设置
Window将该样式应用于所有窗口。style
vs表示未定义名称空间前缀“local”。您必须将其添加到
此处添加您的名称空间
    <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}}"/>