65.9K
CodeProject 正在变化。 阅读更多。
Home

在 Silverlight 中向组合框内添加复选框

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1投票)

2012年9月4日

CPOL

1分钟阅读

viewsIcon

60897

downloadIcon

1377

在 Silverlight 中向组合框内添加复选框。

引言

本文档将帮助您在 Silverlight 中创建一个带有复选框列表的组合框。 在本文中,我使用依赖属性 (DependencyProperty)、INotifyPropertyChangedItemTemplate 来实现复选框的选中和取消选中功能。

控件外观如下

Checkbox inside Combobox

背景 

在本文中,我使用依赖属性来更改 ComboBox 控件中的复选框值。 依赖属性是一种静态方法,用于更改实例化对象属性的值。 我还使用了 INotifyPropertyChanged 来更改自定义用户控件中的属性值。

任何人都可以将此控件用于他们的 Silverlight 项目。 此外,当您将 ItemsSource 绑定到数据时,ItemTemplate 特别有用,它绑定到组合框中的复选框。 在此示例中,当用户选中复选框时,时间将更新到组合框选定的值中,并且当取消选中任何复选框时,时间也会反映在选定的值中。

在此示例中,我创建了一个 Model 类,其中包含 NotifyProperty 并返回用于将其绑定到控件的列表。

在此示例中,我还使用 ItemTemplate 在组合框内显示复选框。 当页面加载时,将结果分配给 ItemsSource。 我还添加了一个 PropertyChanged 事件,用于在选中或取消选中复选框时进行通知。 当调用 PropertyChanged 事件时,时间将绑定的复选框值绑定到组合框选定的值。

我的 MainPage.xaml 如下所示

<grid removed="White" x:name="LayoutRoot">
    <stackpanel margin="10">
        <combobox horizontalalignment="Left" width="200" 
              height="25" name="cmbDepartment">
            <combobox.itemtemplate>
                <datatemplate>
                    <checkbox ischecked="{Binding IsChecked,Mode=TwoWay}" 
                       content="{Binding DepartmentName}">
                </checkbox></datatemplate>
            </combobox.itemtemplate>
        </combobox>
        <textblock margin="5,-20,0,0" horizontalalignment="Left" 
           width="Auto" height="20" x:name="tbDepartment">
    </textblock></stackpanel>
</grid>

我的 MainPage.xaml.cs 如下所示

public partial class MainPage : UserControl
{

    public MainPage()
    {
        InitializeComponent();
        ObservableCollection<department> objDeploymentList = new ObservableCollection<department>();
        cmbDepartment.SelectionChanged += new SelectionChangedEventHandler(cmbDepartment_SelectionChanged);
        DepartmentList obj = new DepartmentList();
        objDeploymentList = obj.LoadDepartmentList();
        cmbDepartment.ItemsSource = objDeploymentList;
        foreach (Department item in objDeploymentList)
        {
            item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
        }
        if (cmbDepartment.ItemsSource != null)
        {
            SetString();
        }
    }

    void cmbDepartment_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        cmbDepartment.SelectedItem = null;
    }

    void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "IsChecked")
        {
            SetString();
        }
    }
    //Function for Bind Selected Value in Combobox
    public void SetString()
    {
        string str = "";
        foreach (Department item in cmbDepartment.ItemsSource)
        {
            if (item.IsChecked)
            {
                str = str + item.DepartmentName + ",";
            }
        }
        if (str != "")
            str = str.Substring(0, str.Length - 1);
        tbDepartment.Text = str;
    }
}
© . All rights reserved.