【问题标题】:Databinding Button with Style带样式的数据绑定按钮
【发布时间】:2012-06-30 15:43:22
【问题描述】:

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

【问题讨论】:

  • 我建议你从一个简单的例子开始,看看数据绑定是如何工作的。你开始有点大了。
  • 还有什么理由你在代码中创建按钮?通常最好在 xaml 中执行它们
  • stackoverflow.com/questions/11274402/… 这是我简单的数据绑定示例

标签: c# .net wpf data-binding


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-26
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 2017-03-18
    • 1970-01-01
    相关资源
    最近更新 更多