扩展的wpf工具包checklistbox [英] extended wpf toolkit checklistbox

查看:274
本文介绍了扩展的wpf工具包checklistbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是否是提出有关扩展wpf工具包的问题的最佳位置,但我试一试; - )。


我有一个CheckListBox我希望限制为一个选择。

所以我创建了一个由ItemSelectionChanged触发的函数

 private void lstFileType_ItemSelectionChanged(object sender ,Xceed.Wpf.Toolkit.Primitives.ItemSelectionChangedEventArgs e)
{
//为了防止永无止境的循环我们首先要检查它是不是代码本身正在检查或取消选中
if(systemChecking == false)
{
//检查我们是否检查了一个项目。我们不允许选择项目,因为必须选择一个项目
var item = e.Item;
// if(e.IsSelected)//选中的新项目,因此取消选中旧项目
// {
foreach(lstFileType.Items中的var数据)
{
if(item!= data&& e.IsSelected)//取消选中未点击
的所有项目{
systemChecking = true;
lstFileType.SelectedItems.Remove(data);
}
else if(item == data&&!e.IsSelected)
{
systemChecking = true;
lstFileType.SelectedItems.Add(data);
}
}
}
其他
{
e.Handled = true;
systemChecking = false;
}
}

我循环所有项目,如果选中一个新值,我取消选中所有其他项目,当然只能一个item。

因为unchecking正在触发事件,所以我使用systemChecking布尔值来阻止代码再次运行。


这一切都很完美,但是当我取消选中一个值时,问题就开始了。因为我总是想要选择一个项目,所以我必须阻止用户取消选择这一项。因此,如果我看到该操作取消选中该项目,我会再次检查它。看起来
简单易行,但是如果我运行这个代码并取消选中一个项目,我会进入一个永无止境的循环,因为事件一直在触发,他继续想要取消选中该项目。


任何帮助或想法都是完美的。

谢谢你
Filip

解决方案

< blockquote>

Hi FilipTop,


如果您只想从CheckListBox中选择一个项目,可以查看以下代码:

 private void CheckListBox1_ItemSelectionChanged(object sender,Xceed.Wpf.Toolkit.Primitives.ItemSelectionChangedEventArgs e)
{
if(CheckListBox1.SelectedItems.Count> 1)
{
string item = e.Item as string;
CheckListBox1.SelectedItems.Remove(item);
}

}

最好的问候,


Cherry



I'm not sure if this is the best place to ask questions about the extended wpf toolkit but I give it a try ;-).

I have a CheckListBox that I want to limit to one selection.
So I made a function that gets triggerd by the ItemSelectionChanged

private void lstFileType_ItemSelectionChanged(object sender, Xceed.Wpf.Toolkit.Primitives.ItemSelectionChangedEventArgs e)
        {
            //to prevent a never ending loop we first have to check if it was not the code itself that was checking or unchecking
            if (systemChecking == false)
            {
                //check if we checked an item. We don't allow to diselect an item because always one item has to be selected
                var item = e.Item;
                //if (e.IsSelected) //the selected a new item, so uncheck the old item
                //{
                foreach (var data in lstFileType.Items)
                {
                    if (item != data && e.IsSelected) //uncheck all the items that the didn't click on
                    {
                        systemChecking = true;
                        lstFileType.SelectedItems.Remove(data);
                    }
                    else if (item == data && !e.IsSelected)
                    {
                        systemChecking = true;
                        lstFileType.SelectedItems.Add(data);
                    }
                }
            }
            else
            {
                e.Handled = true;
                systemChecking = false;
            }
        }

I loop all the items and if the checked a new value than I uncheck all the other items, with ofcourse can be only one item.
Because the unchecking is triggering the event, I use the systemChecking boolean to prevent the code from running again.

All this is working perfect, but the problems starts when I'm unchecking a value. Because I always want one item selected I have to prevent a user from unselecting this one item. So if I see that the action was unchecking the item, I check it again. Looks all simple and easy, but if I run this code and uncheck an item I get in a never ending loop because the event keep on getting triggerd, he keeps on wanting to uncheck the item.

Any help or ideas would be perfect.
Thanks
Filip

解决方案

Hi FilipTop,

If you just want to select only one item from CheckListBox, you can take a look the following code:

 private void CheckListBox1_ItemSelectionChanged(object sender, Xceed.Wpf.Toolkit.Primitives.ItemSelectionChangedEventArgs e)
        {
          if(CheckListBox1.SelectedItems.Count>1)
            {
                string item = e.Item as string;
                CheckListBox1.SelectedItems.Remove(item);                             
            }
           
        }

Best Regards,

Cherry


这篇关于扩展的wpf工具包checklistbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆