How to find element in visual tree? wp7 - c#

How I can find element that contains in App.xaml, grid with name "audioPanel"?
I tried:
Grid found = this.FindChild<Grid>(^*I can't find anything suitable*^, "audioPanel");
How can I find WPF controls by name or type?
UPD: App.xaml http://pastebin.com/KfWbjMV8

UPDATE: You need a combination of both my answer and H.B.'s answer. Use the version of FindChild below, and change your call to FindChild to look like
var grid = FindChild<Grid>(Application.Current.RootVisual, "audioPanel");
Since you're styling the phone application frame, the "control on which it is applied" from H.B.'s comment is pretty likely to be the RootVisual (there may be exceptions to this, I'm not sure).
Also, I'm assuming that the "..." parts of your App.xaml in pastebin have a ContentPresenter in there somewhere, otherwise I don't think your style will work.
END UPDATE
If you're using the accepted answer in the question you linked to (WPF ways to find controls) and your 'audioPanel' grid is nested inside of another grid, then you still won't find it - there's an error in that code. Here's an updated version that will work even if the control is nested:
public static T FindChild<T>(DependencyObject parent, string childName)
where T : DependencyObject
{
// Confirm parent and childName are valid.
if (parent == null)
{
return null;
}
T foundChild = null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
// If the child is not of the request child type child
var childType = child as T;
if (childType == null)
{
// recursively drill down the tree
foundChild = FindChild<T>(child, childName);
// If the child is found, break so we do not overwrite the found child.
if (foundChild != null)
{
break;
}
}
else if (!string.IsNullOrEmpty(childName))
{
var frameworkElement = child as FrameworkElement;
// If the child's name is set for search
if (frameworkElement != null && frameworkElement.Name == childName)
{
// if the child's name is of the request name
foundChild = (T) child;
break;
}
// Need this in case the element we want is nested
// in another element of the same type
foundChild = FindChild<T>(child, childName);
}
else
{
// child element found.
foundChild = (T) child;
break;
}
}
return foundChild;
}
}

If it is in App.xaml i would assume it to be part of a resource in Application.Resources, as resources that are not used anywhere are not in the visual tree this won't do.
If this is true you can try getting the root of the object from the resources and search from there, e.g.
var root = Application.Current.Resources["MyKey"] as FrameworkElement;
Grid found = this.FindChild<Grid>(root, "audioPanel");

just for completeness, the version of E. Z. Hart has a bug, as found sub childs are overwritten. here is a working version
public static T FindChild<T>(this DependencyObject parent, string childName = null) where T : DependencyObject
{
// Confirm parent and childName are valid.
if (parent == null)
return null;
T foundChild = null;
var childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (var i = 0; foundChild == null && i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
// If the child is not of the request child type child
var childType = child as T;
if (childType == null)
{
// recursively drill down the tree
foundChild = FindChild<T>(child, childName);
}
else if (!string.IsNullOrEmpty(childName))
{
var frameworkElement = child as FrameworkElement;
// If the child's name is set for search
if (frameworkElement != null && frameworkElement.Name == childName)
{
// if the child's name is of the request name
foundChild = (T)child;
}
else
{
// Need this in case the element we want is nested
// in another element of the same type
foundChild = FindChild<T>(child, childName);
}
}
else
{
// child element found.
foundChild = (T)child;
}
}
return foundChild;
}

Related

Determining collapsed groups of a list view

Is there any way to determine (and if possible programmatically set) which groups are collapsed and which are not in a list view. Here's how the listview grouping is set up:
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(LvMslInfoTable.ItemsSource);
PropertyGroupDescription groupDescription = new PropertyGroupDescription("GroupObject");
view.GroupDescriptions.Add(groupDescription);
view.SortDescriptions.Add(new SortDescription("GroupObjectSortOrder", ListSortDirection.Ascending));
It took me a while to get to work back at that project. The Link from #Oleg was indeed helpful in that it pointed to the Expander control. The only thing missing was to get into the visual tree and find that element. From then on it was relatively easy to find the missing parts. Here is the code I used in the end. The GetDescendantByType method was copied from somewhere here in Stackoverflow as well. The rest was storing the expanded state in conjunction with a group name (expandedStateStatus is a Dictionary containing those entries).
StackPanel sp = (StackPanel)GetDescendantByType(LvMslInfoTable, typeof(StackPanel));
foreach (var gi in sp.Children.Cast<GroupItem>())
{
var tb = (TextBlock)GetDescendantByType(gi, typeof(TextBlock));
if (tb == null) continue;
Expander exp = (Expander)GetDescendantByType(gi, typeof(Expander));
if (exp == null) continue;
if (!expandedStateStatus.ContainsKey(tb.Text))
{
expandedStateStatus.Add(tb.Text, exp.IsExpanded);
}
}
private static Visual GetDescendantByType(Visual element, Type type)
{
if (element == null) return null;
if (element.GetType() == type) return element;
Visual foundElement = null;
if (element is FrameworkElement frameworkElement)
{
frameworkElement.ApplyTemplate();
}
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
{
Visual visual = VisualTreeHelper.GetChild(element, i) as Visual;
foundElement = GetDescendantByType(visual, type);
if (foundElement != null)
{
break;
}
}
return foundElement;
}

how to find dynamically created CheckBox checked in wpf

I created Check Box dynamically inside a wrap panel in wpf. How to find the checked check box in code behind.
If you want a more compact not reusable and specialized version..
WrapPanel wp = new WrapPanel();
for (int i = 0; i < 10; i++)
{
CheckBox chb = new CheckBox();
chb.Name = string.Format("Id{0}", i);
wp.Children.Add(chb);
}
foreach (CheckBox el in wp.Children)
{
if (el.Name == "Id3")
{
return el;
}
}
/// <summary>
/// Finds a Child of a given item in the visual tree.
/// </summary>
/// <param name="parent">A direct parent of the queried item.</param>
/// <typeparam name="T">The type of the queried item.</typeparam>
/// <param name="childName">x:Name or Name of child. </param>
/// <returns>The first parent item that matches the submitted type parameter.
/// If not matching item can be found,
/// a null parent is being returned.</returns>
public static T FindChild<T>(DependencyObject parent, string childName)
where T : DependencyObject
{
// Confirm parent and childName are valid.
if (parent == null) return null;
T foundChild = null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
// If the child is not of the request child type child
T childType = child as T;
if (childType == null)
{
// recursively drill down the tree
foundChild = FindChild<T>(child, childName);
// If the child is found, break so we do not overwrite the found child.
if (foundChild != null) break;
}
else if (!string.IsNullOrEmpty(childName))
{
var frameworkElement = child as FrameworkElement;
// If the child's name is set for search
if (frameworkElement != null && frameworkElement.Name == childName)
{
// if the child's name is of the request name
foundChild = (T)child;
break;
}
}
else
{
// child element found.
foundChild = (T)child;
break;
}
}
return foundChild;
}
If you are looking for the text of the selected checkbox do somthing like thes:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
string a =checkBox1.Text;
}

How to add new node when building Binary Tree

I'm working with an alternate version of the BinaryTree class called RedBlackTree. I'm trying to build the tree with this add method.
public void Add(T val)
{
RedBlackNode<T> newNode = _rootNode;
while(newNode != null)
{
if(val.CompareTo(newNode.Data) < 0)
{
newNode = newNode.LeftChild;
}
else if(val.CompareTo(newNode.Data) >= 0)
{
newNode = newNode.RightChild;
}
}
newNode.Data = val; //nullReferenceException thrown here <---
newNode.IsBlack = false;
FixColors(newNode);
}
_rootNode is a private field giving the root of the tree. It is initialized to null as given by specific instructions. This Add method is being called from within another class from a method that is reading in file info. On the first iteration, a nullReferenceException is thrown, I assume because the node I am trying to change is null. I don't know how else I am supposed to change the data value of these nodes. I will post the code to the rest of the program if necessary.
Well, the while loop isn't going to exit while newNode isn't null. That implies newNode is going to be null once the loop exits. I think what you want is to create a new node if the RightChild or LeftChild nodes is null, and set newNode to that.
In addition, as Partha's answer points out, you need to initialize the _rootNode value if it isn't already set.
I don't really know how the parent property of the node is set, so I'm going to assume the parent node is injected as a constructor argument of the child node:
public void Add(T val)
{
RedBlackNode<T> parent = _rootNode;
RedBlackNode<T> target = null;
while(target == null)
{
if(val.CompareTo(parent.Data) < 0)
{
if (parent.LeftChild == null)
target = parent.LeftChild = new RedBlackNode<T>(parent);
else
parent = parent.LeftChild;
}
else if(val.CompareTo(newNode.Data) >= 0)
{
if (parent.RightChild == null)
target = parent.RightChild = new RedBlackNode<T>(parent);
else
parent = parent.RightChild;
}
}
target.Data = val;
target.IsBlack = false;
FixColors(newNode);
}
You need to create the node before you access the other fields. So if _rootNode is empty you create using new keyword.
public void Add(T val)
{
if(_rootNode == null)
{
RedBlackNode<T> newNode = new RedBlackNode<T>();
newNode.Data = val;
newNode.IsBlack = false;
_rootNode = newNode;
return;
}
//Followed by your logic
}

Loop an UIElement variable name

I try to loop the names of my food objects in an WPF application.
I want the following names of the foodelement: food0, food1, foot2 ...
for (int i = 0; i < foods.Count; i++)
{
System.Windows.UIElement foodelement = "food" + i; // ERROR here
Canvas.SetLeft(foodelement, foods[i].Position.X);
}
I get the error:
Cannot implicitly convert type string to System.Windows.UIElement
This may help you
System.Windows.UIElement foodelement = (System.Windows.UIElement)YourContainer.FindName("food" + i);
Where YourContainer is the the container(grid/canvas/stack panel/) that holds the UIElements.
Or else you can use the following:
System.Windows.UIElement foodelement = UIHelper.FindChild<System.Windows.UIElement>(Application.Current.MainWindow, "food" + i);
I think Following code will help you
/// <summary>
/// Finds a Child of a given item in the visual tree.
/// </summary>
/// <param name="parent">A direct parent of the queried item.</param>
/// <typeparam name="T">The type of the queried item.</typeparam>
/// <param name="childName">x:Name or Name of child. </param>
/// <returns>The first parent item that matches the submitted type parameter.
/// If not matching item can be found,
/// a null parent is being returned.</returns>
public static T FindChild<T>(DependencyObject parent, string childName)
where T : DependencyObject
{
// Confirm parent and childName are valid.
if (parent == null) return null;
T foundChild = null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
// If the child is not of the request child type child
T childType = child as T;
if (childType == null)
{
// recursively drill down the tree
foundChild = FindChild<T>(child, childName);
// If the child is found, break so we do not overwrite the found child.
if (foundChild != null) break;
}
else if (!string.IsNullOrEmpty(childName))
{
var frameworkElement = child as FrameworkElement;
// If the child's name is set for search
if (frameworkElement != null && frameworkElement.Name == childName)
{
// if the child's name is of the request name
foundChild = (T)child;
break;
}
}
else
{
// child element found.
foundChild = (T)child;
break;
}
}
return foundChild;
}
And use this method
for (int i = 0; i < foods.Count; i++)
{
System.Windows.UIElement ui = FindChild<Button>(Application.Current.MainWindow, "food" + i);
}

Get next item in a tree

Having a tree (logical in DB) with items in the form
List item A
List item B
List item C
List item D
List Item E
List Item F
List Item G
and so on (nesting depth not limited), I want to get the next node down (or up), starting from an arbitrary node.
Let's say, List Item D is given I want to write a function GetNextNode() that would return List Item E.
My idea would be to do some recursion stuff, but maybe there is a more clever way to handle this?
My question:
How would you solve this?
EDIT 1:
The tree can be accessed with functions like:
GetParentNode()
GetChildrenNodes()
GetNextSiblingNode()
etc.
So it's similar to e.g. e Windows Forms TreeView.
I've had to do this several times. From memory:
public Node GetBelowNode()
{
if (GetChildrenNodes().count > 0)
return GetChildrenNodes()[0];
else
if (GetNextSiblingNode() != null)
return GetNextSiblingNode();
else
{
Node curr = this;
Node parent;
while (true)
{
parent = curr.GetParentNode();
if (parent == null)
return null;
else
{
if (parent.GetNextSiblingNode() != null)
return parent.GetNextSiblingNode();
else
curr = parent;
}
}
}
}
You can handle this via recursion or... worst xD
I think there are only 3 basic cases:
private string getNext(TreeNode node)
{
if (node.FirstNode != null)
{
return node.FirstNode.Name;
}
else
{
if (node.NextNode != null)
{
return node.NextNode.Name;
}
else if (node.Parent.NextNode != null)
{
return node.Parent.NextNode.Name;
}
}
return "";
}
This doesn't works for every scenario. You should search the parent's next node too. Thanks to Vincent Vancalbergh for the comment ;-)
public Party Next {
get {
if (this.children.Count > 0) return this.children[0];
Party target = this;
do {
if (target.NextSibling != null) return target.NextSibling;
} while ((target = target.Parent) != null);
return null;
}
}
public Party Previous {
get {
if (Parent != null && Parent.children.Count > 0 && this == Parent.children[0]) {
return Parent;
}
Party target = this;
do {
if (target.PreviousSibling != null) { target = target.PreviousSibling; break; }
} while ((target = target.Parent) != null);
if (target != null) {
while (target.children.Count > 0) {
target = target.children[target.children.Count - 1];
}
}
return target;
}
}
Since I got a great reply for the "down" part, I'll added my own "up" part. Maybe it is for some help of you; the up part is similar to:
Get the previous sibling.
If there is a previous sibling, get the deepest child node of this
sibling.
If there is no previous sibling, get the direct parent.
To get the deepest sibling (2.), I use the following code:
function getDeepestChild( page )
dim result
set result = nothing
dim childPages
set childPages = page.ChildPages
if childPages.Count>0 then
dim p
set p = childPages(childPages.Count-1)
' recurse.
set result = getDeepestChild( p )
else
set result = page
end if
set getDeepestChild = result
end function
(Yes, I do know that this is VBScript; I needed it in fact in this language)
Try this maybe:
TreeNode currentNode = treeView1.SelectedNode;
treeView1.selectedNode = currentNode.NextNode;

Categories