Silverlight CheckBox Results
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.
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.
Any suggestions?
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
开发者_Python百科{
_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" />
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)
Create a list of checkboxes, bound to a collection of objects containing your check property.
Then you are just iterating the collection for all the ones with a Checked value of true. You can bind the display text to a second property if that is what you mean by "content" of the checkbox.
*Note: for the bindings to work properly (accept external updates) the category properties have to be notify properties.
CategoryView.cs
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace PersonTests
{
public partial class CategoryView : UserControl
{
public ObservableCollection<Category> Categories { get; set; }
public CategoryView()
{
InitializeComponent();
this.Categories = new ObservableCollection<Category>()
{
new Category() {CategoryName = "Category 1"},
new Category() {CategoryName = "Category 2"},
new Category() {CategoryName = "Category 3"},
new Category() {CategoryName = "Category 4"}
};
this.DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var checkedCategories = from c in Categories
where c.Checked
select c;
foreach (var category in checkedCategories)
{
// Do something with the selected categories
}
}
}
}
CategoryView.xaml
<UserControl x:Class="PersonTests.CategoryView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot">
<StackPanel>
<Button Content="Click" Click="Button_Click"/>
<ListBox ItemsSource="{Binding Categories}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Checked, Mode=TwoWay}" Content="{Binding CategoryName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
</UserControl>
Category.cs
using System.ComponentModel;
namespace PersonTests
{
public class Category : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool _checked;
public bool Checked
{
get { return _checked; }
set
{
if (_checked != value)
{
_checked = value;
SendPropertyChanged("Checked");
}
}
}
private string _categoryName;
public string CategoryName
{
get { return _categoryName; }
set
{
if (_categoryName != value)
{
_categoryName = value;
SendPropertyChanged("CategoryName");
}
}
}
public virtual void SendPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
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>
And the click event:
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);
}
}
}
Edit: The correct way.
<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>
And binding code:
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; }
}
}
精彩评论