WPF Buttons Style

13,805

Solution 1

If the image is fix you can hard-code it in the style, and use the Content property of Button bin to the Content of 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>

Solution 2

just try this

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

Note: Use ContentPresenter instead of TextBlock if you have to display anything other than flat text

Share:
13,805
Polaris
Author by

Polaris

C# , VB.NET, WCF, WPF, ASP.NET MVC, ASP.NET Web Forms.

Updated on June 14, 2022

Comments

  • Polaris
    Polaris almost 2 years

    I have WPF Form which has many buttons with the same code. Appearance of all buttons must be the same For example, code for one of these buttons

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

    How can I create one style and use it for all my buttons. All buttons has the same png image, only their text different. How can I do this. I tried to do this with Style object in Resource Section:

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

    But this code not work. Can any body know how can I do this?

  • Polaris
    Polaris about 14 years
    It works. But not as I want. I have application level style in ResourceDictionary. Your style replace my Style. Main thing I want to show my png and specific text, but not clean out my main button style
  • Ed Gonzalez
    Ed Gonzalez about 14 years
    Polaris, Using a UserControl and setting the button's content to this new control may work for you. Updated my answer.
  • Polaris
    Polaris about 14 years
    In a stackPanel I have TextBlock. TextBlock Text must be the specific for each button. With userControl version I cannot change TextBlock text.
  • Ed Gonzalez
    Ed Gonzalez about 14 years
    Sure you can, you'll just expose a Dependency property from the new UserControl called ButtonText, or something similar.