我有一个StackPanel
添加具有相同样式的不同按钮的地方。我唯一要更改的是每个按钮内的标签文本。我所做的是创建 Button,分配样式并将其添加到 Stackpanel。现在,我想要的是对应对象的数据绑定。我读了一篇关于数据绑定的文章,但我不明白。
风格:
<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#代码:
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);