如何在wpf应用程序中设置所有窗口的背景图像 [英] How to set the background image of all windows in wpf application

查看:24
本文介绍了如何在wpf应用程序中设置所有窗口的背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to set the background image of all windows and style of all data grids in all windows of my wpf application in a single place

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

I have the following code in my app.xaml file

<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的设计器中,但当我在调试模式下启动应用程序时,它没有显示在任何窗口中.

The style of DataGrid is applied as desired in designer and also when i start debugging The background image appears in designer in visual studio as desired but it is not shown in any window when i start the application in debugging mode.

当我在mainwindow.xaml"文件中添加以下几行时,背景图像会根据需要显示

when I add the following lines in 'mainwindow.xaml' file the background image appears as desired

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

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

I have an icons folder in my project in which the background image is placed with the name of clinic.jpg

推荐答案

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

This is not issue with your ImageBrush, rather you cannot set any property by default style using the base type Window here. Reason is your Window is of type MainWindow not Window. Hence it does not work. If you give your Window Type you will be able to set it for that 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

So basically you will have to define style for all your window types. You can define one style and can simply create others using 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}}"/>

这篇关于如何在wpf应用程序中设置所有窗口的背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆