WPF填充列表,使用要传递的自定义变量分配点击事件处理程序

时间:2011-10-10 18:23:27

标签: c# wpf user-interface listbox

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

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

这是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)"放在代码中代码具有正确事件处理程序的代码中。

2 个答案:

答案 0 :(得分:4)

sender投射为按钮并获取其DataContext,或使用Command并将CommandParameter绑定到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}" ... />

答案 1 :(得分:0)

您在点击处理程序上可以执行的操作是:

private void choose_house(object sender, RoutedEventArgs e)
{
    var button = sender as Button;
    if (button != null)
    {
        var yourObject = button.DataContext as house;
        if (yourObject != null)
        {
            //do stuff with your button
        }
    }
}