代码改变世界

Syncfusion 复选框 ComboBoxAdv

2014-11-18 13:54  stoneniqiu  阅读(679)  评论(0编辑  收藏  举报

    XAML:

    

 <syncfusion:GridTemplateColumn.EditTemplate>
                                            <DataTemplate DataType="viewModel:CustomerViewModel" >
                                                <syncfusion:ComboBoxAdv ItemsSource="{x:Static viewModel:RecordStatusModel.RecordStatusItem}" DropDownOpened="ComboBoxAdvStatus_DropDownOpened" DropDownClosed="ComboBoxAdvStatus_DropDownClosed"
                                                AllowMultiSelect="True">
</syncfusion:ComboBoxAdv> </DataTemplate> </syncfusion:GridTemplateColumn.EditTemplate>

   1。绑定数据源。一个静态的List<string>即可。

   2.关键的两个事件:DropDownOpened和DropDownClosed。 即打开和关闭。在复选框打开的时候初始化,关闭的时候处理用户的选择。

  

  /// <summary>
        /// 报警级别复选框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ComboBoxAdvStatus_DropDownOpened(object sender, EventArgs e)
        {
            var cbatmp = (ComboBoxAdv)sender;
            var alarmtypeselect = new ObservableCollection<object>();
            var targetuser = CustomersDatagrid.SelectedItem as CustomerViewModel;
            if (targetuser != null)
            {
                var liststr = RecordStatusModel.AlarmStatusToStrList(targetuser.RecordTypeFlags);
                foreach (var str in liststr)
                {
                    alarmtypeselect.Add(str);
                }
                cbatmp.SelectedItems = alarmtypeselect;
            }
        }

        /// <summary>
        /// 报警类型复选框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ComboBoxAdvStatus_DropDownClosed(object sender, EventArgs e)
        {
            ComboBoxAdv cbatmp = (ComboBoxAdv)sender;
            var tmp = cbatmp.SelectedItems.ToList<string>();
            var targetuser = CustomersDatagrid.SelectedItem as CustomerViewModel;
            var flag = RecordStatusModel.AlarmStrListToEnum(tmp);
            if (targetuser != null) targetuser.RecordTypeFlags = flag;
            alarmHandleViewModel.Save();
        }
RecordStatusModel:
static class RecordStatusModel
    {
       private static List<string> _recordStatusItem;
       private static Dictionary<AlarmStatusFlags, string> _alarmStatusDictionary;

       public static List<string> RecordStatusItem
       {
           get { return _recordStatusItem??(_recordStatusItem=new List<string>()
           {
                "报警",
                "应答",
                "恢复",
                "移除",
           }); }
       }

       public static Dictionary<AlarmStatusFlags, string> AlarmStatusDictionary
       {
           get
           {
               return _alarmStatusDictionary ?? (_alarmStatusDictionary = new Dictionary<AlarmStatusFlags, string>()
               {
                {AlarmStatusFlags.Alarm, "报警"},
                {AlarmStatusFlags.Acked, "应答"},
                {AlarmStatusFlags.Recover, "恢复"},
                {AlarmStatusFlags.Remove, "移除"},
               });
           }
       }

       public static List<string> AlarmStatusToStrList(AlarmStatusFlags flags)
       {
           return (from dic in AlarmStatusDictionary where (int)(dic.Key & flags) > 0 select dic.Value).ToList();
       }

       public static AlarmStatusFlags AlarmStrListToEnum(IEnumerable<string> list)
       {
           return (from str in list from dic in AlarmStatusDictionary where dic.Value == str select dic)
               .Aggregate<KeyValuePair<AlarmStatusFlags, string>, AlarmStatusFlags>(0, (current, dic) => current | dic.Key);
       }
    }
View Code