带风格的数据绑定按钮

时间:2012-06-30 15:43:22

标签: c# .net wpf data-binding

我有一个StackPanel,我在其中添加了相同风格的不同按钮。我唯一想要改变的是每个Button内的标签文本。我所做的是创建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);

1 个答案:

答案 0 :(得分:0)

MSDN向您展示了如何在代码中进行绑定:http://msdn.microsoft.com/en-us/library/ms742863.aspx

你的ControlTemplate看起来有些奇怪。正如Charleh所说,通常最好在xaml中进行绑定。

如果要将ControlTemplate中Label的文本绑定到Buttons Content,则必须引入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>

如果你有某种类的实现INotifyPropertyChanged带有你想要绑定的属性,就像这样:

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));
      }
   }
}

您可以尝试使用msdn-link中的代码绑定,如下所示:

Button EventListUIEventButton = new Button();  
MyData myDataObject = new MyData();
Binding myBinding = new Binding("MyDataProperty");
myBinding.Source = myDataObject;
EventListUIEventButton.SetBinding(ContentProperty, myBinding);