Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 使整个应用程序中的按钮样式相同_Wpf_Button - Fatal编程技术网

Wpf 使整个应用程序中的按钮样式相同

Wpf 使整个应用程序中的按钮样式相同,wpf,button,Wpf,Button,我正在开发WPF窗口应用程序。我是WPF新手。我想在整个应用程序中使用相同的按钮样式。我知道我们需要在app.xaml中编写样式。我可以为简单的按钮编码,但我想为这个按钮设置图像 请建议app.xaml文件的任何链接/示例代码,以便它适用于整个应用程序,因为您必须覆盖常规按钮模板。在App.Xaml(或任何更合适的位置)中定义以下内容: <Application.Resources> <ResourceDictionary> <Style TargetType=

我正在开发WPF窗口应用程序。我是WPF新手。我想在整个应用程序中使用相同的按钮样式。我知道我们需要在app.xaml中编写样式。我可以为简单的按钮编码,但我想为这个按钮设置图像


请建议app.xaml文件的任何链接/示例代码,以便它适用于整个应用程序

,因为您必须覆盖常规按钮模板。在App.Xaml(或任何更合适的位置)中定义以下内容:

<Application.Resources>
 <ResourceDictionary>

<Style TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ButtonBase" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
                        <Border>
                        <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="24"></ColumnDefinition>
                                    <ColumnDefinition Width="*"></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <Image Source="../Resources/Images/foo.png"></Image>
                                <ContentPresenter Grid.Column="1" RecognizesAccessKey="True" Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" Margin="{TemplateBinding Control.Padding}" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                            </Grid>
                        </Border>                            
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
 </ResourceDictionary>
</Application.Resources>

此样式没有键,因此应用于所有按钮实例。请注意,您必须设置显式类型,但不能为ButtonBase设置显式类型

请注意,这将删除按钮chrome(将其替换为简单边框),如果要保留原始chrome,请首先抓住原始按钮模板并相应地修改它(在ContentPresenter周围环绕一个网格或DockPanel并添加图像)


您可以使用诸如“WPF控制模板查看器”或Expression Blend之类的工具获取原始模板。

了解如何为按钮制作
ControlTemplate
。。。此示例没有
,但是,您应该能够轻松地修改示例源以添加一个。。。因为你想在每个按钮中使用相同的图像,这是一个相对简单的添加,我把它留给你作为一个学习练习。使用
ControlTemplates

查看一下也可能有用。您想在所有按钮上设置相同的图像吗?或者在diff按钮上显示diff图像?或者您可以从这里获得样式: