WPF按钮样式

Pol*_*ris 2 wpf styles

我有WPF表单,其中有许多按钮具有相同的代码.所有按钮的外观必须相同例如,其中一个按钮的代码

<Button x:Name="btnAddRelative" Width="120" Click="btnAddRelative_Click"  >
    <Button.Content>
        <StackPanel Orientation="Horizontal">
            <Image Height="26" HorizontalAlignment="Left">
                  <Image.Source>
                      <BitmapImage UriSource="images/add.png" />
                  </Image.Source>
            </Image>
            <TextBlock Text="  Add Relative" Height="20" VerticalAlignment="Center"/>
        </StackPanel>
    </Button.Content>
</Button>
Run Code Online (Sandbox Code Playgroud)

如何创建一个样式并将其用于我的所有按钮.所有按钮都具有相同的png图像,只有它们的文本不同.我怎样才能做到这一点.我尝试使用资源部分中的Style对象执行此操作:

<UserControl.Resources>
    <Style TargetType="Button" x:Key="AddStyle">
        <Setter Property="Content">
            <Setter.Value>
                <StackPanel Orientation="Horizontal">
                    <Image Height="26" HorizontalAlignment="Left">
                        <Image.Source>
                            <BitmapImage UriSource="images/add.png" />
                        </Image.Source>
                    </Image>
                    <TextBlock Text="  " Height="20" VerticalAlignment="Center"/>
                </StackPanel>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

但是这段代码不起作用.任何人都可以知道我该怎么办?

Dra*_*ake 7

在样式如果图像修复可以硬编码,并利用内容的属性按钮斌到内容文本框

 <Style x:Key="ButtonStyle" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border
                            Background="{TemplateBinding Background}"                            
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                            <StackPanel 
                                Orientation="Horizontal">
                                <!--<Image Height="26" HorizontalAlignment="Left">
                                    <Image.Source>
                                        <BitmapImage UriSource="images/add.png" />
                                    </Image.Source>
                                </Image>-->
                                <TextBlock 
                                    Foreground="{TemplateBinding Foreground}"
                                    Text="{TemplateBinding Content}" 
                                    Height="20" 
                                    VerticalAlignment="{TemplateBinding VerticalAlignment}"/>
                            </StackPanel>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
Run Code Online (Sandbox Code Playgroud)