1

I am a beginner in C#. I was trying to set some properties to my button in my visual C# WPF application using the following xaml:

<Grid>
    <Button Panel.ZIndex="1" Content="Button" Height="37" HorizontalAlignment="Stretch"
            Margin="378,12,38,262" Name="button1" VerticalAlignment="Stretch" Width="87" >
        <Button.Resources>
            <Style x:Key="buttonStyle">
                <Setter Property="Button.Background" Value="Gray"/>
                <Setter Property="Button.Foreground" Value="White"/>
                <Setter Property="Button.FontFamily" Value="Comic Sans MS"/>
            </Style>
        </Button.Resources>
    </Button>

    <Image Margin="0,0,0,0" Name="image1">
        <Image.Source>
            <BitmapImage UriSource="images.jpg"></BitmapImage>
        </Image.Source>
    </Image>
</Grid>

But the Button.Resource has no effect on the style of the button. Can anyone please tell me the problem in this

2
  • 1
    Look more into Styles and Triggers with WPF. Commented Apr 9, 2014 at 9:43
  • I read "Comic Sans MS". :( :( :( :( Commented Apr 9, 2014 at 9:44

4 Answers 4

1

Replace

<Button Panel.ZIndex="1" Content="Button" Height="37" HorizontalAlignment="Stretch"
        Margin="378,12,38,262" Name="button1" VerticalAlignment="Stretch" Width

with

<Button Style="{DynamicResource buttonStyle}" Panel.ZIndex="1"
Content="Button" Margin ="169,84,34,0" Name="button1" Height="23"
VerticalAlignment="Top">
Sign up to request clarification or add additional context in comments.

Comments

1
  <Window.Resources>
        <Style x:Key="ButtonStyle1" 
               TargetType="{x:Type Button}">
            <Setter Property="Button.Background" Value="Gray"/>
            <Setter Property="Button.Foreground" Value="White"/>
            <Setter Property="Button.FontFamily" Value="Comic Sans MS"/>
        </Style>
    </Window.Resources>
    <Grid>
        <Button Panel.ZIndex="1" Content="Button" Height="37" HorizontalAlignment="Stretch"
            Margin="378,12,38,262" Name="button1" VerticalAlignment="Stretch" Style="{StaticResource ButtonStyle1}" Width="87" >
        </Button>
        <Image Margin="0,0,0,0" Name="image1">
        </Image>
    </Grid>

Comments

1

You create explicit Button style but you don't use it anywhere. You can either set these values directly against button

<Button ... Background="Gray" Foreground="White" FontFamily="Comic Sans MS"/>

or, if you want to create explicit Style, as you do, you need to use it on your Button

<Grid>
    <Grid.Resources>
       <Style x:Key="buttonStyle" TargetType="{x:Type Button}">
           <Setter Property="Background" Value="Gray"/>
           <Setter Property="Foreground" Value="White"/>
           <Setter Property="FontFamily" Value="Comic Sans MS"/>
       </Style>
    </Grid.Resources>
    <Button ... Style="{StaticResource buttonStyle}"/>
</Grid>

or you can create implicit style, that will automatically apply to child Button controls in current visual tree

<Grid>
    <Grid.Resources>
       <Style TargetType="{x:Type Button}">
           <Setter Property="Background" Value="Gray"/>
           <Setter Property="Foreground" Value="White"/>
           <Setter Property="FontFamily" Value="Comic Sans MS"/>
       </Style>
    </Grid.Resources>
    <Button .../>
</Grid>

or create Style just for that Button

<Button>
   <Button.Style>
      <Style TargetType="{x:Type Button}">
         <Setter Property="Background" Value="Gray"/>
         <Setter Property="Foreground" Value="White"/>
         <Setter Property="FontFamily" Value="Comic Sans MS"/>
      </Style>
   </Button.Style>
</Button>

Comments

0

You can write this way:

        <Button Panel.ZIndex="1" Content="Button" Height="37" HorizontalAlignment="Stretch" 
            Margin="378,12,38,262" x:Name="button1" 
            VerticalAlignment="Stretch" Width="87" 
            Background="Gray" Foreground="White" FontFamily="Comic Sans MS" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.