2012-06-30 47 views
0

我有一個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); 
+0

我建議你從一個簡單的例子開始,看看數據綁定如何工作。你開始有點大。 – MBen

+0

也有什麼理由你在代碼中創建按鈕?它通常是更好地做他們在XAML – Charleh

+0

http://stackoverflow.com/questions/11274402/binding-in-textblock-doesnt-work-in-wpf/11274438#comment14826300_11274438 這在我的數據的簡單示例結合 – Charleh

回答

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結合 - 像這樣的鏈接:

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