I have a StackPanel
where I add different buttons with the same style. The only thing I want to change is the text of the Labels inside each Button. What I do is I create the Button, assign the style and add it to the Stackpanel. Now, what I want to have is a Databinding to the corresponding object. I read an article about Databinding but i dont get it.
Style:
<Style x:Key="EventListUIEventButtonStyle"
TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Name="EventListUIEventButtonGrid"
ShowGridLines="false"
Height="60px"
Margin="0,5,0,0">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="45px" />
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="40px" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0"
Grid.Column="2"
Grid.ColumnSpan="2"
Grid.RowSpan="1"
VerticalAlignment="Bottom"
HorizontalAlignment="Right"
Foreground="white"
FontSize="16"
FontWeight="bold">
00:01:12
</Label>
<Label Name="EventListUIEventButtonLabel01" Grid.Row="0"
Grid.Column="1"
Grid.ColumnSpan="1"
Grid.RowSpan="2"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Margin="0 5 0 0"
Foreground="black"
FontSize="22"
FontWeight="bold">
Test
</Label>
<Label Grid.Row="2"
Grid.Column="1"
Grid.ColumnSpan="1"
Grid.RowSpan="1"
VerticalAlignment="Bottom"
HorizontalAlignment="Center"
Foreground="white"
FontSize="12">
Restaurant
</Label>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
C# code:
Button EventListUIEventButton = new Button();
EventListUIEventButton = new Button();
EventListUIEventButton.Style = (Style)MainWindow.FindResource("EventListUIEventButtonStyle");
EventListUIEventButton.Background = new SolidColorBrush(this.GetFunctionColor(Event.Function));
// Here i want to set the databinding (Databinding: corresponding Object)
this.StackPanel.Children.Add(EventListUIEventButton);