【问题标题】:Silverlight CheckBox ResultsSilverlight 复选框结果
【发布时间】:2011-07-29 10:45:42
【问题描述】:

我对 Silverlight 有点陌生。我在一个页面上有大约 12 个复选框。我想要完成的是,当用户点击一个按钮时,它必须返回哪些 CheckBoxes 已经被选中。而且我还需要复选框的内容。

我曾考虑将相同的 Checked Event 添加到所有 CheckBox 等,但从那里我并不完全确定我将使用它。

有什么建议吗?

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

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)

【问题讨论】:

    标签: silverlight checkbox


    【解决方案1】:

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

    然后,您只是为所有 Checked 值为 true 的集合迭代。如果复选框的“内容”是您的意思,您可以将显示文本绑定到第二个属性。

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

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

    【讨论】:

    • 很好的答案,完整的代码真的很有帮助。执行此操作时绝对要使用绑定,而不是代码隐藏。
    【解决方案2】:

    正确的方法是@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; }
        }
    }
    

    【讨论】:

    • 嘿,谢谢,这绝对看起来对我有用。这是一个小应用程序,所以这是完美的。非常感谢!
    • @Melanie:注意:如果双向绑定要接受外部更新,则属性应该是通知属性,但 anivas 的基本版本将用于复选框列表,而不会做任何其他事情。
    猜你喜欢
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多