У меня есть StackPanel
, где я добавляю разные кнопки с тем же стилем. Единственное, что я хочу изменить, это текст ярлыков внутри каждой кнопки. То, что я делаю, я создаю Button, назначаю стиль и добавляю его в Stackpanel. Теперь, что я хочу, это привязка к соответствующему объекту. Я прочитал статью о Databinding, но я не понимаю.Кнопка привязки данных со стилем
Стиль:
<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);
Я предлагаю начать с простого примера, чтобы увидеть, как работает привязка данных. Вы начали немного больше. – MBen
Также есть причина, по которой вы создаете кнопки в коде? Это, как правило, лучше делать их в XAML – Charleh
http://stackoverflow.com/questions/11274402/binding-in-textblock-doesnt-work-in-wpf/11274438#comment14826300_11274438 Это в моем простом примере связывания данных – Charleh