Silverlight CheckBox结果

时间:2011-07-29 10:45:43

标签: silverlight checkbox

我对Silverlight有点新鲜。我在页面上有大约12个CheckBoxes。我想要完成的是,当用户点击一个按钮时,它必须返回已经检查过的CheckBox。而且我还需要CheckBox的内容。

我曾想过将相同的Checked事件添加到所有CheckBoxes等,但从那里开始我并不完全确定我要去哪里。

有什么建议吗?

我有一个包含字符串属性的类,此时这些属性与Checkboxes相关......这就是我开始的方式,不确定它是否正确。

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" />

对于每个CheckBox(5),我的xaml看起来都是一样的。然后我只在页面上有一个按钮。当用户点击此按钮时,我想知道哪些复选框已被检查(即CateogryOne,CategoryThree和CategoryFour)

2 个答案:

答案 0 :(得分:1)

创建一个复选框列表,绑定到包含check属性的对象集合。

然后,您只是迭代所有Checked值为true的集合。如果您的意思是&#34; content&#34;的复选框。

* 注意:要使绑定正常工作(接受外部更新),类别属性必须是通知属性。

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));
            }
        }
    }
}

答案 1 :(得分:0)

正确的方法是@HiTech通过数据绑定建议的。如果你想尝试做代码隐藏(我不推荐),可以这样做:

<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; }
    }
}