0

I want to stick to the DRY principle, so I have about 20 buttons in a grid, and they all have the same margin, they look like this:

<Button Grid.Column="0" Grid.Row="0" Margin="3"/>
<Button Grid.Column="0" Grid.Row="1" Margin="3"/>
<Button Grid.Column="0" Grid.Row="2" Margin="3"/>
<Button Grid.Column="0" Grid.Row="3" Margin="3"/>
<Button Grid.Column="0" Grid.Row="4" Margin="3"/>
<Button Grid.Column="1" Grid.Row="0" Margin="3"/>
<Button Grid.Column="1" Grid.Row="1" Margin="3"/>
<Button Grid.Column="1" Grid.Row="2" Margin="3"/>
<Button Grid.Column="1" Grid.Row="3" Margin="3"/>
<Button Grid.Column="1" Grid.Row="4" Margin="3"/>
<Button Grid.Column="2" Grid.Row="0" Margin="3"/>
<Button Grid.Column="2" Grid.Row="1" Margin="3"/>
<Button Grid.Column="2" Grid.Row="2" Margin="3"/>
<Button Grid.Column="2" Grid.Row="3" Margin="3"/>
<Button Grid.Column="2" Grid.Row="4" Margin="3"/>
<Button Grid.Column="3" Grid.Row="0" Margin="3"/>
<Button Grid.Column="3" Grid.Row="1" Margin="3"/>
<Button Grid.Column="3" Grid.Row="2" Margin="3"/>
<Button Grid.Column="3" Grid.Row="3" Margin="3"/>
<Button Grid.Column="3" Grid.Row="4" Margin="3"/>

I want to add a variable called something like "ButtonMargin", set it to 3, and then set the margin of each of these buttons to ButtonMargin because in case I want to change it in the future this way I'll only change one variable instead of 20.

I tried having an actual variable inside the .cs file string ButtonMargin; and set it to "3" inside the constructor, but I get an invalid markup error when writing Margin = ButtonMargin or Margin = this.ButtonMargin inside the XAML file.

How do I do it properly?

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Lastrevio2
  • 51
  • 6
  • See marked duplicate for information on using styles. Note that you could also do this programmatically with a variable as you've tried to, but you need to get the markup correct: the source value needs to come from a property, you need to use `{Binding}` markup, and you need to reference the object and its property in the binding markup. – Peter Duniho Sep 09 '19 at 16:21

1 Answers1

1

WPF already has built-in functionality for this. It's called styling.

Create a Style for your button and define all the properties you want to have the same values.

Like this:

<Grid>
    <Grid.Resources>
        <Style TargetType="Button">
            <Setter Property="Margin" Value="2"/>
        </Style>
    </Grid.Resources>

    <Button/>
    <Button/>
</Grid>

Now, all the buttons in that Grid will get the same margin.

dymanoid
  • 14,771
  • 4
  • 36
  • 64
  • Thanks! Why is it in this case Grid.Resources instead of Grid.Style like in this article? https://www.wpf-tutorial.com/styles/using-styles/ – Lastrevio2 Sep 09 '19 at 16:01
  • 1
    Please read that article carefully. By placing a style in the resources, you define a common style that will be applied to all children of the `FrameworkElement` where the resource is defined. By applying a style directly, you only specify the style for that concrete `FrameworkElement`. – dymanoid Sep 09 '19 at 16:03