WPF中如何改变win7样式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF中如何改变win7样式相关的知识,希望对你有一定的参考价值。

比如在wpf中创建一个按钮,用imagebrush来为按钮指定背景图,当鼠标位于按钮之上时改变背景图片,但是win7自带的控件样式会将我想指定的背景图完全覆盖,就是那种青色半透明的,原来用winform时只要装了win7也会是这个情况,请问有什么方法解决吗?
请问二楼所说的ButtonChrome在哪啊,我在Expression blend4中只在杂项中看到了Template,能否帮忙截个图啊,还有就是手写xaml怎么写啊,谢谢啦!

改按钮的ControlTemplate
你用Blend打开,会看到里面放了一个ButtonChrome,换成Border即可
或者你直接手写Xaml也行

<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="TemplateBinding Background"
BorderBrush="TemplateBinding BorderBrush"
BorderThickness="TemplateBinding BorderThickness"
CornerRadius="2">
<ContentPresenter ContentSource="Content"
ContentTemplate="TemplateBinding ContentTemplate"
ContentStringFormat="TemplateBinding ContentStringFormat"
ContentTemplateSelector="TemplateBinding ContentTemplateSelector"
HorizontalAlignment="TemplateBinding HorizontalContentAlignment"
VerticalAlignment="TemplateBinding VerticalContentAlignment"
Margin="TemplateBinding Padding" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

Template就是ControlTemplate
改Template是WPF的基本功,建议多加练习
参考技术A 重装

WPF listBox选中项改变样式

<UserControl x:Class="Mjj.MyUserControls.LeftMenu"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Loaded="UserControl_Loaded">
    <UserControl.Resources>

        <Style x:Key="LeftMenuListBoxStyle" TargetType="ListBox">
            <Setter Property="ItemContainerStyle">
                <Setter.Value>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                    <Border x:Name="Border" Height="46px" BorderThickness="0 0 0 1" BorderBrush="#E4E7ED"  VerticalAlignment="Center">
                                        <Label x:Name="Label" VerticalAlignment="Center" HorizontalAlignment="Center"  FontSize="14px" Foreground="#202122" >
                                            <ContentPresenter></ContentPresenter>
                                        </Label>
                                    </Border>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsSelected" Value="True">
                                            <Setter TargetName="Border" Property="BorderBrush" Value="#0089FF"></Setter>
                                            <Setter TargetName="Border" Property="Background" Value="#D9E0EE"></Setter>
                                            <Setter TargetName="Label" Property="Foreground" Value="#F2901C" ></Setter>
                                            <Setter TargetName="Border" Property="BorderThickness" Value="0 0 1 0"></Setter>
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>

                       
                    </Style>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <ListBox Style="{StaticResource LeftMenuListBoxStyle}" DisplayMemberPath="MenuName"  BorderThickness="0" Width="160px" Name="ListBoxLeftMenu"  >

    </ListBox>
</UserControl>

 

以上是关于WPF中如何改变win7样式的主要内容,如果未能解决你的问题,请参考以下文章

Windows API编程(不是MFC)用CreateWindowEx创建的按钮等子窗口控件是默认的Windows经典样式,如何改变?

WPF 透明窗体,无边框(比如一些桌面的日历桌面程序),如何让程序钉在桌面上。直接镶嵌在桌面背景上?

WPF宽度100%,我想wpf的子控件的宽度跟父控件的宽度一样,当父控件宽度改变的时候子控件随着改变,请问有啥!

怎么改变WPF中DataGrid的行样式

如何在 WPF 中禁用按钮上的 MouseOver 效果?

WPF中的TreeView,如何改变子节点的顺序, 给个例子,多谢