0

我有一个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);
4

1 回答 1

0

MSDN shows you, how to do binding in code here: http://msdn.microsoft.com/en-us/library/ms742863.aspx .

Your ControlTemplate looks some kind of strange. As Charleh said, usually it would be better to do the binding in xaml.

If you want to bind the text of a Label within your ControlTemplate to the Buttons Content, you have to introduce an TemplateBinding.

 <Style x:Key="EventListUIEventButtonStyle" TargetType="Button"> 
    <Setter Property="Template"> 
      <Setter.Value> 
        <ControlTemplate TargetType="Button"> 
          <Grid ...>
            ...
           <Label Content="{TemplateBinding Content}" ... />
            ...
          </Grid> 
        </ControlTemplate> 
    </Setter.Value> 
  </Setter>

If you have some kind of class the implements INotifyPropertyChanged with a property you want to bind to, like this:

public class MyData : INotifyPropertyChanged
{
   private string myDataProperty;

   public MyData()
   {
       myDataProperty = "I like to be a label text!";
   }

   public string MyDataProperty
   {
      get { return myDataProperty; }
      set
      {
        myDataProperty = value;
        OnPropertyChanged("MyDataProperty");
      }
   }

   public event PropertyChangedEventHandler PropertyChanged;

   private void OnPropertyChanged(string info)
   {
      PropertyChangedEventHandler handler = PropertyChanged;
      if (handler != null)
      {
        handler(this, new PropertyChangedEventArgs(info));
      }
   }
}

you can try the in code binding from the msdn-link like this:

Button EventListUIEventButton = new Button();  
MyData myDataObject = new MyData();
Binding myBinding = new Binding("MyDataProperty");
myBinding.Source = myDataObject;
EventListUIEventButton.SetBinding(ContentProperty, myBinding);
于 2012-06-30T23:41:13.467 回答