【问题标题】:Creating a WPF button with a dynamic name in XAML在 XAML 中创建具有动态名称的 WPF 按钮
【发布时间】:2009-09-17 17:44:56
【问题描述】:

我在 WPF 中有一个绑定的 ListBox,其中每个项目都有一个向上/向下按钮,用于在列表框中向上或向下移动项目。

但是我需要知道哪个按钮触发了事件,所以我想将按钮的名称设置为“UpButton”+ listBoxItem.Text 类型的东西。

这是 XAML 的简化版本

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Column="0" Text="{Binding Path=Position}"/>
                <Label Grid.Column="1"  Content="{Binding Path=Name}" FontSize="18" Margin="0,10,0,0" />
                <WrapPanel Grid.Column="2" >
                    <Button Click="MoveUpClick" Name="UpButton">Up</Button>
                    <Button Click="MoveDownClick" Name="DownButton">Down</Button>
                </WrapPanel>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

如果有人知道提供类似排序功能和/或使用 DragDrop 对其进行排序的控件,我将不胜感激任何帮助,因为我已经逾期 2 个小时才能去度假。

【问题讨论】:

  • 马克,为什么你需要知道按钮的名称?这似乎不是“真正的 WPF 方式”...
  • (别误会我的意思,我只是想帮忙,只是在 WPF 中可能有更好的方法来解决这个问题,而不需要文本)
  • 每个 ListBoxItem 有两个按钮,所以当我收到 ClickEvent 时,我需要知道哪个 ListBoxItem 与我单击的按钮“关联”。我几乎可以肯定,有一种更优雅的方法可以做到这一点,只是我的时间很短。如果有人能解释这样做的正确方法,我会很高兴。

标签: wpf xaml dynamic button


【解决方案1】:

正如 gimalay 所说,在 Click 事件处理程序中,您可以从发送者的数据上下文中获取关联的项目(作为数据模板的一部分)。

Button senderButton = sender as Button;
var item = senderButton.DataContext;

对于这样一个简单的问题,Joseph 的建议可能有点过头了。

【讨论】:

    【解决方案2】:

    在事件处理程序中 Sender(button).DataContext 属性将被设置为 ItemsSource 项。 如果您想获得一些与 Sender 相关的控制权,请使用 VisualTreeHelper。

    【讨论】:

      【解决方案3】:

      就像 Yacoder 所说,肯定有一种更优雅的方式来做到这一点。但是,如果您想使用动态名称方法,您应该能够使用附加属性使其工作,如下所示:

      namespace ListBoxExample
      {
          public static class TagAttach
          {
              public static readonly System.Windows.DependencyProperty TagProperty = 
                  System.Windows.DependencyProperty.RegisterAttached(
                  "Tag", typeof (string), typeof (TagAttach));
              public static void SetTag(System.Windows.UIElement element, string value)
              {
                  element.SetValue(TagProperty, value);
              }
              public static string GetTag(System.Windows.UIElement element)
              {
                  return (string)element.GetValue(TagProperty);
              }
          }
      }
      

      <Window x:Class="ListBoxExample.Window1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:loc="clr-namespace:ListBoxExample"
          Title="Window1" Height="300" Width="300">
          <Grid>
              <ListBox ItemsSource="{Binding}" Grid.IsSharedSizeScope="True">
                  <ListBox.ItemTemplate>
                      <DataTemplate>
                          <Grid>
                              <Grid.ColumnDefinitions>
                                  <ColumnDefinition SharedSizeGroup="A" />
                                  <ColumnDefinition SharedSizeGroup="B" />
                                  <ColumnDefinition />
                              </Grid.ColumnDefinitions>
      
                              <TextBlock Grid.Column="0" Text="{Binding Position}"/>
                              <Label Grid.Column="1"  Content="{Binding Name}" FontSize="18" Margin="0,10,0,0" />
                              <WrapPanel Grid.Column="2" >
                                  <Button Click="MoveUpClick" loc:TagAttach.Tag="{Binding Name}">Up</Button>
                                  <Button Click="MoveDownClick" loc:TagAttach.Tag="{Binding Name}">Down</Button>
                              </WrapPanel>
                          </Grid>
                      </DataTemplate>
                  </ListBox.ItemTemplate>
              </ListBox>
          </Grid>
      </Window>
      

      namespace ListBoxExample
      {
          public partial class Window1
          {
              public Window1()
              {
                  InitializeComponent();
                  DataContext = new[]
                      {
                          new {Name = "Tom", Position = "Butcher"},
                          new {Name = "Dick", Position = "Baker"},
                          new {Name = "Harry", Position = "Candlestick Maker"}
                      };
              }
      
              private void MoveUpClick(object sender, System.Windows.RoutedEventArgs e)
              {
                  System.Windows.MessageBox.Show("Up - " + TagAttach.GetTag(sender as System.Windows.UIElement));
              }
      
              private void MoveDownClick(object sender, System.Windows.RoutedEventArgs e)
              {
                  System.Windows.MessageBox.Show("Down - " + TagAttach.GetTag(sender as System.Windows.UIElement));
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        • 1970-01-01
        • 2017-01-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多