I have a number of list box controls which I would like to clear using a loop rather than typing individually.
I was able to do this quite easily in winforms but cannot fathom how to achieve the same under wpf.
The closest I have got is this:
As above it won't compile because "Error 1 'System.Windows.Controls.Control' does not contain a definition for 'Items' and no extension method 'Items' accepting a first argument of type 'System.Windows.Controls.Control' could be found."
if I change the foreach from control to listbox then it will compile, but then at runtime it fails with the unhandled exception "Additional information: Unable to cast object of type 'System.Windows.Controls.DataGrid' to type 'System.Windows.Controls.ListBox'."
Could anyone help me out here?
I was able to do this quite easily in winforms but cannot fathom how to achieve the same under wpf.
The closest I have got is this:
C#:
foreach (Control c in this.gdLayout.Children)
{
if (c.GetType() == typeof(ListBox)) //where control is of type list box
{
//Clear items
c.Items.Clear();
}
}
As above it won't compile because "Error 1 'System.Windows.Controls.Control' does not contain a definition for 'Items' and no extension method 'Items' accepting a first argument of type 'System.Windows.Controls.Control' could be found."
if I change the foreach from control to listbox then it will compile, but then at runtime it fails with the unhandled exception "Additional information: Unable to cast object of type 'System.Windows.Controls.DataGrid' to type 'System.Windows.Controls.ListBox'."
Could anyone help me out here?