C# 清除所有组合框方法
我正在尝试创建一个方法,该方法将清除我窗口上的所有C# 清除所有组合框方法,c#,wpf,combobox,C#,Wpf,Combobox,我正在尝试创建一个方法,该方法将清除我窗口上的所有组合框。这就是我迄今为止所尝试的: private void ClearAllComboboxes(ComboBox cmb) { cmb.SelectedIndex = -1; } 然后我调用下面的方法,但一次只能插入一个组合框来清除 private void btnClearAll_Click(object sender, RoutedEventArgs e) { ClearAllComboboxes(cmbBarlocks
组合框。这就是我迄今为止所尝试的:
private void ClearAllComboboxes(ComboBox cmb)
{
cmb.SelectedIndex = -1;
}
然后我调用下面的方法,但一次只能插入一个组合框来清除
private void btnClearAll_Click(object sender, RoutedEventArgs e)
{
ClearAllComboboxes(cmbBarlocks);
}
所以我要做的是用尽可能少的编码清除所有的组合框。有人能告诉我怎么做,最好的方法是什么?谢谢:)在您的处理程序中试试这个,当我在ListBox中遇到类似问题时,它就起作用了
void ClearCombos(params ComboxBox[] boxes)
{
foreach(var box in boxes)
box.ItemsSource = null;
}
并称之为ClearCombos(x,y,z);其中,x、y、z是要清除的框让我假设所有的组合框
都在一个容器中(设为stackPanel
),您可以使用以下代码片段将所有框的选定索引设置为-1:
foreach (Control ctrl in stkContainer.Children)
{
if (ctrl.GetType() == typeof(ComboBox))
{
ComboBox cbo = ctrl as ComboBox;
ClearAllComboboxes(cbo);
}
}
如果要清除组合框,则意味着您必须将方法签名重新定义为:
private void ClearAllComboboxes(ComboBox cmb)
{
cmb.Items.Clear();
}
我假设您正在使用MVVM,并且您已经为viewmodel中的每个组合框选择了EdItem属性。
在viewmodel中,您只需为每个组合框设置SelectedItem=null。
它将清除您的组合框选择
如果您未使用MVVM,则可以在代码隐藏中使用以下代码:
private void ClearAllComboboxes()
{
List<ComboBox> comboBoxes = new List<ComboBox>();
GetLogicalChildCollection<ComboBox>(container, comboBoxes);
comboBoxes.ForEach(combobox => combobox.SelectedIndex = -1);
}
private static void GetLogicalChildCollection<T>(DependencyObject parent,List<T> logicalCollection) where T : DependencyObject
{
var children = LogicalTreeHelper.GetChildren(parent);
foreach (object child in children)
{
if (child is DependencyObject)
{
DependencyObject depChild = child as DependencyObject;
if (child is T)
{
logicalCollection.Add(child as T);
}
GetLogicalChildCollection(depChild, logicalCollection);
}
}
}
private void clearallcomboxes()
{
列表组合框=新列表();
GetLogicalChildCollection(容器、组合框);
combobox.ForEach(combobox=>combobox.SelectedIndex=-1);
}
私有静态void GetLogicalChildCollection(DependencyObject父对象,List logicalCollection),其中T:DependencyObject
{
var children=LogicalTreeHelper.GetChildren(父级);
foreach(子对象中的对象子对象)
{
if(子对象是DependencyObject)
{
DependencyObject depChild=作为DependencyObject的子对象;
如果(孩子是T)
{
logicalCollection.Add(子级为T);
}
GetLogicalChildCollection(depChild,logicalCollection);
}
}
}
这将如何帮助我一次清除所有组合框?感谢您的评论:)我收到您的编码错误:无法将类型为“System.Windows.Controls.Image”的对象强制转换为类型为“System.Windows.Controls.Control”。?感谢您的评论!在哪里可以获得控件集合
?这是WPF还是WinForms谢谢你的评论,伙计!:D我收到错误“使用泛型类型'System.Collections.generic.IEnumerable'需要1个类型参数”您能告诉我确切的行吗?请检查您是否添加了对System.Collections.generic的引用。我还编辑了答案,看看它是否能解决您的错误。谢谢
private void ClearAllComboboxes()
{
List<ComboBox> comboBoxes = new List<ComboBox>();
GetLogicalChildCollection<ComboBox>(container, comboBoxes);
comboBoxes.ForEach(combobox => combobox.SelectedIndex = -1);
}
private static void GetLogicalChildCollection<T>(DependencyObject parent,List<T> logicalCollection) where T : DependencyObject
{
var children = LogicalTreeHelper.GetChildren(parent);
foreach (object child in children)
{
if (child is DependencyObject)
{
DependencyObject depChild = child as DependencyObject;
if (child is T)
{
logicalCollection.Add(child as T);
}
GetLogicalChildCollection(depChild, logicalCollection);
}
}
}