代码之家  ›  专栏  ›  技术社区  ›  Mark

在XAML中创建具有动态名称的WPF按钮

  •  2
  • Mark  · 技术社区  · 17 年前

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

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

    <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个小时。

    3 回复  |  直到 17 年前
        1
  •  1
  •   Trainee4Life    17 年前

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

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

        2
  •  0
  •   gimalay    17 年前

    在事件处理程序发送器(按钮)中。DataContext属性将设置为ItemsSource item。 如果您想获得某种与发送者相关的控制,请使用VisualTreeHelper。

        3
  •  0
  •   Joseph Sturtevant    17 年前

    正如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));
            }
        }
    }
    
    推荐文章