如何读取动态添加的复选框?
我将复选框动态添加到 silverlight stackpanel 对象,如下所示:
foreach (String userName in e.Result)
{
CheckBox ch = new CheckBox();
ch.Name = userName;
ch.Content = userName;
ContentStackPanel.Children.Add(ch);
}
如何读回这些控件以检测其中哪些控件被选中。
I am adding checkboxes dynamically to silverlight stackpanel object as follows:
foreach (String userName in e.Result)
{
CheckBox ch = new CheckBox();
ch.Name = userName;
ch.Content = userName;
ContentStackPanel.Children.Add(ch);
}
How do I read back those controls to detect which of them are checked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用数据绑定到复选框列表。像这样的事情:
使用列表框创建检查列表:
然后在您的代码中只需将 chklist itemSource 设置为带有您的对象的 ObservableCollection
You can use databinding to checkbox list. Something like this:
Use a Listbox to create the check list:
Then in your code just set the chklist itemSource to an ObservableCollection with your object
您可能应该避免在这样的代码中创建复选框。可能对您有用的是复选框的迷你“ViewModel”。像这样的事情:
然后,您可以拥有这些选项的集合,如下所示:
填充后,您可以将 ObservableCollection 绑定到 ItemsControl:
XAML 将为您添加到选项集合中的所有选项创建复选框。真正伟大的是,您现在可以询问选项集合已选择哪些选项:
使用数据绑定和模板是您应该在 Silverlight/WPF 中熟悉的技术。这是一个非常重要的概念,它将让您在应用程序中做出惊人的事情。
You should probably avoid creating checkboxes in code like this. Something that might be useful for you is a mini "ViewModel" for the checkbox. Something like this:
Then, you can have a collection of these options like this:
Once this is populated, you can bind the ObservableCollection to an ItemsControl:
That XAML will create the CheckBoxes for you for ever option you added to your options collection. The really great thing is that you can now ask you options collection which options have been selected:
Using data binding and templates is a technique you should get familiar with in Silverlight/WPF. It is a really important concept, and it will let you do amazing things in you application.