c#以编程方式创建列表事件处理程序howto

时间:2021-08-30 00:03:01

Can anyone tell me how to connect a click event of a checkbox inside of a programatically created list? So far I have only seen examples of connecting events of objects created statically and not dynamically allocated from code.

任何人都可以告诉我如何连接一个以编程方式创建的列表中的复选框的单击事件?到目前为止,我只看到了连接静态创建的对象事件的示例,而不是从代码动态分配的对象。

the list is created as described in the snippet below:

列表的创建方式如下面的代码段所示:

namespace CustomListApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            for (int i = 1; i <= 10; ++i)
            {
                CustomListBoxItem clbi = new CustomListBoxItem();
                clbi.Title.Text = "This is item " + i;
                clbi.Condition.IsChecked = i % 2 == 0;
                CustomListBox.Items.Add(clbi);
            }
        }
    }
}

and the CustomListBoxItem has this structure:

并且CustomListBoxItem具有以下结构:

<UserControl x:Class="CustomListApp.CustomListBoxItem"[...]>

    <Grid x:Name="ListBoxItemGrid" Background="{StaticResource PhoneChromeBrush}" Height="65">
        <CheckBox x:Name="Condition" Height="67" HorizontalAlignment="Right" VerticalAlignment="Top" Width="65" Click="Condition_Click" />
        <TextBlock x:Name="Title" Height="65" HorizontalAlignment="Left" Margin="84,0,0,0" Text="TextBlock" VerticalAlignment="Center" Width="294" />
    </Grid>
</UserControl>

2 个解决方案

#1


1  

perhaps you can create a typ of your elements which you want to add to your listbox. And then you can check for a click like this:

也许您可以创建要添加到列表框中的元素类型。然后你可以检查这样的点击:

<YOUR DATA TYPE> data = (sender as ListBox).SelectedItem as <YOUR DATA TYPE>;

Then you can work with this data. I hope that I could help you with this answer.

然后,您可以使用此数据。我希望我能帮助你解决这个问题。

#2


0  

you mean like

你的意思是

cbi.Condition.Click += new_click_handler;

where new_click_handler is the new click handler you want to add?

new_click_handler是您要添加的新点击处理程序?

but do you need to add a new handler to each item programatically? can't you just reference the item inside that already existing Condition_Click handler you already have in your xaml?

但是你需要以编程方式为每个项添加一个新的处理程序吗?你不能只在你的xaml中已经存在的已经存在的Condition_Click处理程序中引用该项吗?

#1


1  

perhaps you can create a typ of your elements which you want to add to your listbox. And then you can check for a click like this:

也许您可以创建要添加到列表框中的元素类型。然后你可以检查这样的点击:

<YOUR DATA TYPE> data = (sender as ListBox).SelectedItem as <YOUR DATA TYPE>;

Then you can work with this data. I hope that I could help you with this answer.

然后,您可以使用此数据。我希望我能帮助你解决这个问题。

#2


0  

you mean like

你的意思是

cbi.Condition.Click += new_click_handler;

where new_click_handler is the new click handler you want to add?

new_click_handler是您要添加的新点击处理程序?

but do you need to add a new handler to each item programatically? can't you just reference the item inside that already existing Condition_Click handler you already have in your xaml?

但是你需要以编程方式为每个项添加一个新的处理程序吗?你不能只在你的xaml中已经存在的已经存在的Condition_Click处理程序中引用该项吗?