Skip to main content
 首页 » 编程设计

c#之WPF 填充列表,为单击事件处理程序分配自定义变量以传递

2024年02月23日13kenshinobiy

我正在填充一个列表框,每个列表项都有一个按钮。

我已经将文本填充到列表中,但我希望每个按钮都有正确的事件处理程序和要传递给它的门牌号。

这是 XAML 代码:

<Window x:Class="WpfApplication1.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" Background="#FFCBCBCB"> 
    <Grid> 
        <Label Content="Welcome to the house manager" Height="28" HorizontalAlignment="Left" Margin="20,12,0,0" Name="label1" VerticalAlignment="Top" /> 
 
        <ListBox Height="253" HorizontalAlignment="Left" Margin="10,46,0,0" VerticalAlignment="Top" Width="481" x:Name="houseList"> 
            <ListBox.ItemTemplate> 
                <DataTemplate DataType="house"> 
                    <Button Click="choose_house(HOUSE NUMBER HERE)" Background="#FFBEBEBE" BorderThickness="0" Focusable="True" Width="459" BorderBrush="White" Padding="0"> 
                        <StackPanel Orientation="Vertical" Width="400" VerticalAlignment="Stretch"> 
                            <TextBlock Margin="0,5,0,0" Text="{Binding street}" HorizontalAlignment="Left" /> 
                            <TextBlock Margin="0,5,0,0" Text="{Binding postCode}" HorizontalAlignment="Left" /> 
                        </StackPanel> 
                    </Button> 
                </DataTemplate> 
            </ListBox.ItemTemplate> 
        </ListBox> 
    </Grid> 
</Window> 

这是目前填充列表的代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
 
namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
        public MainWindow() 
        { 
            InitializeComponent(); 
        } 
 
        private void Window_Loaded(object sender, RoutedEventArgs e) 
        { 
            List<house> houseAddresses = new List<house>(); 
 
            // Create a new house 
            for (var i = 1; i <= 10; i++) 
            { 
                house newHouse = new house(); 
                newHouse.street = i + " Scale Hall Lane"; 
                newHouse.postCode = "PR4 3TL"; 
                newHouse.houseNumber = i; 
 
                houseAddresses.Add(newHouse); 
 
            } 
 
            // Go through each house and add them to the list 
            foreach (house houses in houseAddresses) 
            { 
                houseList.Items.Add(houses); 
            } 
        } 
 
        private void choose_house(object sender, RoutedEventArgs e) 
        { 
            MessageBox.Show("Clicked"); 
        } 
 
    } 
} 

这是我的家庭类:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
 
namespace WpfApplication1 
{ 
    class house 
    { 
        public string street { get; set; } 
        public string postCode { get; set; } 
        public int houseNumber { get; set; } 
 
    } 
} 

我已将 Click="choose_house(HOUSE NUMBER HERE)" 放在按钮的代码中,代码将具有正确的事件处理程序。

请您参考如下方法:

将您的 sender 转换为 Button 并获取它的 DataContext,或者使用 Command 并将 CommandParameter 绑定(bind)到 HouseNumber

private void choose_house(object sender, RoutedEventArgs e) 
{ 
    var ctrl = sender as Button; 
    var h = ctrl.DataContext as house; // Please capitalize your classes! :) 
 
    MessageBox.Show(h.houseNumber); 
} 

或

<Button Command="{Binding Path=DataContext.ChooseHouseCommand,  
                          RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" 
        CommandParameter="{Binding houseNumber}" ... />