开发者

WPF Populating list, assigning on-click event handler with custom variables to pass

开发者 https://www.devze.com 2023-04-11 20:35 出处:网络
I\'m populating a listBox, Each list item has a button. I\'ve got it populating text in to the list, but I\'d like each button to have the correct event handler and the house number to be passed to i

I'm populating a listBox, Each list item has a button.

I've got it populating text in to the list, but I'd like each button to have the correct event handler and the house number to be passed to it.

Here's the XAML code:

<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>

Here's the code which populates the list at the moment:

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)
        开发者_如何学JAVA{
            MessageBox.Show("Clicked");
        }

    }
}

Here's my house class:

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; }

    }
}

I've put Click="choose_house(HOUSE NUMBER HERE)" in the code on the button where the code would have the correct event handler.


Either cast your sender as a Button and get it's DataContext, or use a Command and bind the CommandParameter to the 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);
}

or

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


What you can do on the click handler is:

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
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号