Silverlight复选框结果 [英] Silverlight CheckBox Results
问题描述
我是Silverlight的新手。我在页面上大约有12个CheckBox。我要完成的是,当用户单击按钮时,它必须返回所有已选中的CheckBox。我还需要CheckBox的内容。
I'm a little new to Silverlight. I have about 12 CheckBoxes on a page. What I want to accomplish is, when the user clicks on a button, it must return which CheckBoxes have all been checked. And I also need the Content of the CheckBox.
我曾经考虑过将相同的Checked Event添加到所有CheckBox等中,但是从那里我并不确定我要去哪里。
I have thought of adding the same Checked Event to all of the CheckBoxes etc, but from there I'm not really entirely sure where I'm going with it.
有什么建议吗?
我有一个包含字符串属性的类,该类属性此时与Checkboxes相关。这只是我开始的方式,不确定是否正确。
I have a class that contains string properties which at this point correlates to the Checkboxes...This is just the way I started, not sure if it's right.
public class Categories : Base
{
#region Constructor
public Categories()
{
}
#endregion
#region Private Properties
private string _CategoryOne = default(string);
private string _CategoryTwo = default(string);
private string _CategoryThree = default(string);
private string _CategoryFour = default(string);
private string _CategoryFive = default(string);
#endregion
#region Public Properties
public string CategoryOne
{
get { return _CategoryOne; }
set
{
_CategoryOne = value;
NotifyPropertyChanged("CategoryOne");
}
}
public string CategoryTwo
{
get { return _CategoryTwo; }
set
{
_CategoryTwo = value;
NotifyPropertyChanged("CategoryTwo");
}
}
public string CategoryThree
{
get { return _CategoryThree; }
set
{
_CategoryThree = value;
NotifyPropertyChanged("CategoryThree");
}
}
public string CategoryFour
{
get { return _CategoryFour; }
set
{
_CategoryFour = value;
NotifyPropertyChanged("CategoryFour");
}
}
public string CategoryFive
{
get { return _CategoryFive; }
set
{
_CategoryFive = value;
NotifyPropertyChanged("CategoryFive");
}
}
#endregion
}
Xaml:
<StackPanel Orientation="Horizontal"
Grid.Row="2"
Grid.Column="2"
Margin="5,1,1,1"
VerticalAlignment="Center">
<CheckBox Content="Category One"
VerticalAlignment="Center"
HorizontalAlignment="Left"
VerticalContentAlignment="Center"
Margin="1,1,3,1" />
我的xaml与每个CheckBox(5)。然后,我在页面上只有一个按钮。当用户单击此按钮时,我想知道已选中了哪些复选框(即CateogryOne,CategoryThree和CategoryFour)
My xaml looks the same for each CheckBox(5). Then I just have a button on the page. And when the User clicks this button I want to know which Checkboxes have been checked (ie CateogryOne, CategoryThree and CategoryFour)
推荐答案
The正确的方法是@HiTech通过数据绑定提出的建议。如果要在尝试执行代码时执行隐藏代码(我不建议这样做),则可以执行以下操作:
The correct way is what @HiTech suggested through databinding. If want to do in code-behind as you are trying to do (which I don't recommend) it can be done as:
<StackPanel x:Name="LayoutRoot" Background="White">
<CheckBox Content="Test 1"/>
<CheckBox Content="Test 2"/>
<CheckBox Content="Test 3"/>
<CheckBox Content="Test 4"/>
<Button Content="Click" Click="Button_Click"/>
</StackPanel>
点击事件:
private void Button_Click(object sender, RoutedEventArgs e)
{
foreach (var item in this.LayoutRoot.Children)
{
var checkbox = item as CheckBox;
if (checkbox != null)
{
System.Diagnostics.Debug.WriteLine("{0} is {1}", checkbox.Content, checkbox.IsChecked);
}
}
}
修改:正确的方法。
<StackPanel x:Name="LayoutRoot" Background="White">
<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked, Mode=TwoWay}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Click" Click="Button_Click"/>
</StackPanel>
并绑定代码:
public partial class MainPage : UserControl
{
private ObservableCollection<CheckBoxItem> items = new ObservableCollection<CheckBoxItem>();
public ObservableCollection<CheckBoxItem> Items
{
get
{
return this.items;
}
}
public MainPage()
{
InitializeComponent();
this.items.Add(new CheckBoxItem() {Name = "Test 1", IsChecked = true});
this.items.Add(new CheckBoxItem() {Name = "Test 2", IsChecked = false});
this.items.Add(new CheckBoxItem() {Name = "Test 3", IsChecked = false});
this.items.Add(new CheckBoxItem() {Name = "Test 4", IsChecked = true});
this.DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
foreach (var item in Items)
{
System.Diagnostics.Debug.WriteLine("{0} is {1}", item.Name, item.IsChecked);
}
}
public class CheckBoxItem
{
public string Name { get; set; }
public bool IsChecked { get; set; }
}
}
这篇关于Silverlight复选框结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!