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

标签 c# wpf user-interface 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)" 放在按钮的代码中,代码将具有正确的事件处理程序。

最佳答案

将您的 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}" ... />

关于c# - WPF 填充列表,为单击事件处理程序分配自定义变量以传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7716984/

相关文章:

c# - Nullable<T> 的装箱/拆箱行为如何可能?

c# - SQLite,具有相同列的多个表或一个大表?什么是更好的?

c# - WPF 列表框通过单击空白区域删除选择

c# - 如何将属性绑定(bind)到 WPF 中 Treeview 中的选定节点

symfony - 如何在 Sonata admin 中为 yaml 配置实现 gui?

linux - * NIX GUI如何工作?

c# - 如何从分组元素创建字典?

c# - 如何在初始化期间访问 UserControl 的 XAML 设置属性?

c# - WPF Treeview 仅展开第一个节点和选定的项目?

java - JTable 非常慢