How do I access the children of an ItemsControl? - c#

If i have a component derived from ItemsControl, can I access a collection of it's children so that I can loop through them to perform certain actions? I can't seem to find any easy way at the moment.

A solution similar to Seb's but probably with better performance :
for(int i = 0; i < itemsControl.Items.Count; i++)
{
UIElement uiElement =
(UIElement)itemsControl.ItemContainerGenerator.ContainerFromIndex(i);
}

See if this helps you out:
foreach(var item in itemsControl.Items)
{
UIElement uiElement =
(UIElement)itemsControl.ItemContainerGenerator.ContainerFromItem(item);
}
There is a difference between logical items in a control and an UIElement.

To identify ItemsControl's databound child controls (like a ToggleButton), you can use this:
for (int i = 0; i < yourItemsControl.Items.Count; i++)
{
ContentPresenter c = (ContentPresenter)yourItemsControl.ItemContainerGenerator.ContainerFromItem(yourItemsControl.Items[i]);
ToggleButton tb = c.ContentTemplate.FindName("btnYourButtonName", c) as ToggleButton;
if (tb.IsChecked.Value)
{
//do stuff
}
}

Related

Find all the child controls in wpf

I would like to find all of the controls within a WPF control specially in Datagrid, DataGridTemplateColumn. I have had a look at a lot of samples and it seems that they all either require a Name to be passed as parameter or simply do not work.
What have you tried that "require a name to be passed or simply don't work" and what's wrong with the former?
private void FindAllChildren()
{
var depObj = dataGrid;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child is DataGridTemplateColumn)
{
// do a thing
}
}
}
Adapted from here: https://stackoverflow.com/a/978352/1189566

Get the UserControl from DataTemplate

I have a DataTemplate in which i have simply used a UserControl.
DataTemplate x:Key="SampleDataTemplate">
<controls1:UserControl1>
</controls1:UserControl1>
</DataTemplate>
This DataTemplate is used in TransitionControl.ContentTemplate Now I want the UserControl1 Object in the c# CodeBehind.
Something Like this
TransitionControl.ContentTemplate this DataTemplate will give me UserControl1 object.
You could use VisualTreeHelper's GetChildrenCount and GetChild methods to get to the control that you need. Here's a method that will help (tweak it if necessary):
private List<T> FindChildren<T>(DependencyObject startNode, List<T> results = null) where T : DependencyObject {
if (results == null) results = new List<T>();
int count = VisualTreeHelper.GetChildrenCount(startNode);
for (int i = 0; i < count; i++) {
DependencyObject current = VisualTreeHelper.GetChild(startNode, i);
if ((current.GetType()).Equals(typeof(T)) || (current.GetType().GetTypeInfo().IsSubclassOf(typeof(T)))) {
T realType = (T)current;
results.Add(realType);
}
FindChildren<T>(current, results);
}
return results;
}
So, all you need to do is call FindChildren<UserControl1>(MyTransitionControlInstance) and you'll get get the instance of the UserControl1 control (well, all of the instances, if there are more).
P.S. It really is a good idea to tweak the method to just look for one element, and not for all of them, if you need just one which seems to be the case.

WPF ContentControl: Iterating over child control

I have a WPF Window, where I put a ContentControl, Later in my code I use a string to be read as Xaml using XamlReader.Load function and put that in ContentControl. This is done to make a Dyanmic UI. Now all is done, but I want to capture the Input field values from this control. on button click.
So, all I want to do is to Iterate on Child Controls of ContentControl. How can I do this, there doesn't seems a way to iterate on child of it? Any Idea. Thanks.
Here, you can use VisualTreeHelper:
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(contentControl); i++)
{
var child = VisualTreeHelper.GetChild(contentControl, i);
if(child is ContentPresenter)
{
var contentPresenter = child as ContentPresenter;
for (int j = 0; j < VisualTreeHelper.GetChildrenCount(contentPresenter); j++)
{
var innerChild = VisualTreeHelper.GetChild(contentPresenter, j);
}
break;
}
}
The difference between the logical and visual tree is, that the visual tree lists all elements which are used to display the control. E.g. The visual tree of a button looks like
Button -> Border -> ContentPresenter -> TextBlock
The logical tree list just the controls itsself (as you declared in your xaml).
For further details, visit this site: http://wpftutorial.net/LogicalAndVisualTree.html
So to get the children you want, LogicalTreeHelper.GetChildren(contentControl); should work.

Get ListView Visible items

I have a ListView which might contains a lot of items, so it is virtualized and recycling items. It does not use sort. I need to refresh some value display, but when there are too many items, it is too slow to update everything, so I would like to refresh only the visible items.
How could I get a list of all currently displayed items ? I tried to look into the ListView or in the ScrollViewer, but I still have no idea how to achieve this. The solution must NOT go through all items to test if they can be seen, because this would be too slow.
I'm not sure code or xaml would be useful, it is just a Virtualized/Recycling ListView with its ItemSource bound to an Array.
Edit :
Answer :
thanks to akjoshi, I found the way :
get the ScrollViewer of the ListView
(with a FindDescendant method, that you can do yourself with the VisualTreeHelper ).
read its ScrollViewer.VerticalOffset : it is the number of the first item shown
read its ScrollViewer.ViewportHeight : it is the count of items shown.
Rq : CanContentScroll must be true.
Have a look at this question on MSDN showing a technique to find out the visible ListView items -
How to find the rows (ListViewItem(s)) in a ListView that are actually visible?
Here's the relevant code from that post -
listView.ItemsSource = from i in Enumerable.Range(0, 100) select "Item" + i.ToString();
listView.Loaded += (sender, e) =>
{
ScrollViewer scrollViewer = listView.GetVisualChild<ScrollViewer>(); //Extension method
if (scrollViewer != null)
{
ScrollBar scrollBar = scrollViewer.Template.FindName("PART_VerticalScrollBar", scrollViewer) as ScrollBar;
if (scrollBar != null)
{
scrollBar.ValueChanged += delegate
{
//VerticalOffset and ViweportHeight is actually what you want if UI virtualization is turned on.
Console.WriteLine("Visible Item Start Index:{0}", scrollViewer.VerticalOffset);
Console.WriteLine("Visible Item Count:{0}", scrollViewer.ViewportHeight);
};
}
}
};
Another thing you should do is to use ObservableCollection as your ItemSource instead of an Array; that will definitely improve the performance.
Update:
Ya that might be true(array vs. ObservableCollection) but I would like to see some statistics related to this;
The real benefit of ObservableCollection is if you have a requirement to add/remove items from your ListView at run-time, in case of an Array you will have to reassign the ItemSource of ListView and the ListView first throws away its previous items and regenerates its entire list.
After trying to figure out something similar, I thought I would share my result here (as it seems easier than the other responses):
Simple visibility test I got from here.
private static bool IsUserVisible(FrameworkElement element, FrameworkElement container)
{
if (!element.IsVisible)
return false;
Rect bounds =
element.TransformToAncestor(container).TransformBounds(new Rect(0.0, 0.0, element.ActualWidth, element.ActualHeight));
var rect = new Rect(0.0, 0.0, container.ActualWidth, container.ActualHeight);
return rect.Contains(bounds.TopLeft) || rect.Contains(bounds.BottomRight);
}
Afterwards you can loop through the listboxitems and use that test to determine which are visible. Since the listboxitems are always ordered the same the first visible one in this list would be the first visible one to the user.
private List<object> GetVisibleItemsFromListbox(ListBox listBox, FrameworkElement parentToTestVisibility)
{
var items = new List<object>();
foreach (var item in PhotosListBox.Items)
{
if (IsUserVisible((ListBoxItem)listBox.ItemContainerGenerator.ContainerFromItem(item), parentToTestVisibility))
{
items.Add(item);
}
else if (items.Any())
{
break;
}
}
return items;
}
How I see things :
on one side, you have your data. They must be up to date, because this is where your information is in memory. Iterating on your data list should be pretty fast, and most of all, can be done on another thread, in background
on the other side, you have the display. Your ListView already make the trick of refreshing only the datas displayed, since it's virtualizing ! You need no more tricks, it's already in place !
On last work, using a binding on an ObservableCollection is a good advice. If you intend to modify the ObservableCollection from an another thread, I would recommend this : http://blog.quantumbitdesigns.com/2008/07/22/wpf-cross-thread-collection-binding-part-1/
I spend a lot of time finding a better solution for this,
In my situation i have a scrollviewer, filled with items with custom heigths that can be set visible/invisible, i came up with this. It does the same as above solutions but with a fraction of the CPU. I hope it helps some one.
The first items of the listview / scrollpanel is TopVisibleItem
public int TopVisibleItem { get; private set; }
private double CurrentDistance;
private void TouchScroller_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
if (myItemControl.Items.Count > 0)
{
MoveDirection direction = (MoveDirection)Math.Sign(e.VerticalChange);
if (direction == MoveDirection.Positive)
while (CurrentDistance < e.VerticalOffset && TopVisibleItem < myItemControl.Items.Count)
{
CurrentDistance += ((FrameworkElement)myItemControl.Items[TopVisibleItem]).ActualHeight;
TopVisibleItem += 1;
}
else
while (CurrentDistance >= e.VerticalOffset && TopVisibleItem > 0)
{
CurrentDistance -= ((FrameworkElement)myItemControl.Items[TopVisibleItem]).ActualHeight;
TopVisibleItem -= 1;
}
}
}
public enum MoveDirection
{
Negative = -1,
Positive = 1,
}
If you have a virtualization enabled ListView, Then you can get all Current Visible items as below:
Get VirtualizingStackPanel
Get all ListViewItems in VirtualizingStackPanel
The code is shown below.
VirtualizingStackPanel virtualizingStackPanel = FindVisualChild<VirtualizingStackPanel>(requiredListView);
List<ListViewItem> items = GetVisualChildren<ListViewItem>(virtualizingStackPanel);
The Functions are shown below.
private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem)
return (childItem)child;
else
{
childItem childOfChild = FindVisualChild<childItem>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
private List<childItem> GetVisualChildren<childItem>(DependencyObject obj) where childItem : DependencyObject
{
List<childItem> childList = new List<childItem>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem)
childList.Add(child as childItem);
}
if (childList.Count > 0)
return childList;
return null;
}
This will return you list of current ListViewItem loaded for displaying.
Hope it helps :).

Silverlight 4 - Visual Tree helper returns 0 child Elements?

I have the following code that is supposed to find an element inside of something. The problem is that I have a DataGrid, whose first column has a CheckBox in it's header. The checkbox itself is defined in a Style, which exists in the <controls:ChildWindow.Resources> dictionary of the parent child window.
At runtime, if all elements of the binding collection of the grid are "selected", than I need to select the damn checkbox. However, since the checkbox exists in the style it is not easily accesible and thus I need to walk through the entire DOM to find the specific checkbox.
This is the code that makes the DOM Traversal. It is implemented as an extension method to FrameworkElement so I can call the FindElement or GetChildren methods from any control that inherits from FrameworkElement:
public static class FrameworkElementExtensions
{
public static FrameworkElement FindElement(this FrameworkElement parentFrameworkElement, string childFrameworkElementNameToSearch)
{
FrameworkElement childFrameworkElementFound = null;
parentFrameworkElement.SearchElements(ref childFrameworkElementFound, childFrameworkElementNameToSearch);
return childFrameworkElementFound;
}
public static List<FrameworkElement> GetChildren(this FrameworkElement parentElement)
{
List<FrameworkElement> childFrameworkElementsFound = new List<FrameworkElement>();
parentElement.GetChildren(childFrameworkElementsFound);
return childFrameworkElementsFound;
}
public static void SearchElements(this FrameworkElement parentFrameworkElement, ref FrameworkElement childFrameworkElementToFind, string childFrameworkElementName)
{
int childrenCount = VisualTreeHelper.GetChildrenCount(parentFrameworkElement);
if (childrenCount > 0)
{
FrameworkElement childFrameworkElement = null;
for (int i = 0; i < childrenCount; i++)
{
childFrameworkElement = (FrameworkElement)VisualTreeHelper.GetChild(parentFrameworkElement, i);
if (childFrameworkElement != null && childFrameworkElement.Name.Equals(childFrameworkElementName))
{
childFrameworkElementToFind = childFrameworkElement;
return;
}
childFrameworkElement.SearchElements(ref childFrameworkElementToFind, childFrameworkElementName);
}
}
}
public static void GetChildren(this FrameworkElement parentFrameworkElement, List<FrameworkElement> allChildFrameworkElement)
{
int childrenCount = VisualTreeHelper.GetChildrenCount(parentFrameworkElement);
if (childrenCount > 0)
{
for (int i = 0; i < childrenCount; i++)
{
FrameworkElement childFrameworkElement = (FrameworkElement)VisualTreeHelper.GetChild(parentFrameworkElement, i);
allChildFrameworkElement.Add(childFrameworkElement);
childFrameworkElement.GetChildren(allChildFrameworkElement);
}
}
}
}
So the issue at hand is that when i call something along the lines of SomeDataGrid.FindElement("HeaderCheckBox"); it always returns a null. The assumption here is that I have a DataGrid called SomeDataGrid and a CheckBox defined within a style called HeaderCheckBox.
Upon further debugging I also found out that no matter what control I call these extension methods from, the VisualTreeHelper.GetChildrenCount method call used in the last two methods in my code always returns 0 ??? WTF?
Anyone has any idea on how to fix this?
Thanks,
Martin
Sure, no problem. Here it is.
It was made with VS2010 and Silverlight 4... just in case.

Categories