WPF按钮样式

时间:2010-04-07 11:41:27

标签: 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>

如何创建一种样式并将其用于所有按钮。所有按钮都具有相同的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>

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

3 个答案:

答案 0 :(得分:7)

如果图片已修复,您可以在样式中对其进行硬编码,并使用按钮 bin的内容属性内容 TextBox

 <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>

答案 1 :(得分:2)

试试这个

   <Window.Resources>
    <Style TargetType="Button"
           x:Key="AddStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <StackPanel Orientation="Horizontal">
                        <Image Height="26"
                               Width="20"
                               HorizontalAlignment="Left">
                            <Image.Source>
                                <BitmapImage UriSource="/WpfApplication33;component/Images/MoveLeft.png" />
                            </Image.Source>
                        </Image>
                        <TextBlock Text ="{TemplateBinding Content}"
                                   Height="20"
                                    />
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</Window.Resources>
<Grid>
    <StackPanel>
    <Button  Style="{StaticResource AddStyle}"
             Height="25" Width="100"  
             Content="Button1"></Button>
    <Button  Style="{StaticResource AddStyle}"
             Height="25"
             Width="100"

             Content="Button22"></Button>
        <Button  Style="{StaticResource AddStyle}"
                 Height="25"
                 Width="100"
                 Content="Button2233"></Button>
        <Button  Style="{StaticResource AddStyle}"
                 Height="25"
                 Width="100"
                 Content="Button2332"></Button>

    </StackPanel>
</Grid>

注意:如果必须显示除平面文本以外的任何内容,请使用ContentPresenter而不是TextBlock

答案 2 :(得分:0)

尝试按以下方式更改样式

<UserControl.Resources>
        <Style
            TargetType="Button"
            x:Key="AddStyle">
            <Setter
                Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <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>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

[受评论启发的编辑]

您可以创建一个新的UserControl,让我们将其称为包含您的Stackpanel的AddButtonContent等,然后将其包含在您的按钮中,如下所示:

<Button>
    <local:AddButtonContent
        ButtonText="Testing, one, two, three" />
</Button>

您需要使用所有按钮向UserControl添加一个名为local(或任何您想要调用它)的xmlns引用。

AddButtonContent UserControl的代码隐藏部分需要以下代码,您需要命名TextBlock(我在本例中使用了testText)。

public static DependencyProperty ButtonTextProperty =         DependencyProperty.Register( “ButtonText”         typeof运算(字符串),         typeof运算(AddButtonContent),         new PropertyMetadata(“”,onTextChangedCallback));

public string ButtonText
{
    get { return (string)GetValue(ButtonTextProperty); }
    set
    {
        SetValue(ButtonTextProperty, value);
    }
}


static void onTextChangedCallback(
    DependencyObject dobj,
    DependencyPropertyChangedEventArgs args)
{
    AddButtonContent abc = dobj as AddButtonContent;
    abc.testText.Text = args.NewValue.ToString();
}