i got this loop here:
foreach (object obje in this.treeViewSL.Items)
{
TreeViewItem TVI = (TreeViewItem)treeViewSL.ItemContainerGenerator.ContainerFromItem(obje);
if ((TVI.Header.ToString() == _object.NodeText))
{
TVI.IsSelected = true;
TVI.IsExpanded = true;
_selectedItem = treeViewSL.SelectedItem as TreeViewItem;
continue;
}
}
this works fine to look if there is treeviewitem with this header in the first items, but it seems that treeviewsl.Items doesn't count the childs, it gives me 1 back but there are 3, so 1 root with 2 childs, how i can loop through these also?
Related
I am applying a datasource to a sublayout and obtaining the values of its children as follows:
Sitecore.Collections.ChildList childItems;
if (Sitecore.Context.Database.GetItem(this.DataSource) != null)
{
childItems = Sitecore.Context.Database.GetItem(this.DataSource).GetChildren();
}
else
{
litDataSourceError.Text += "You need to set a datasource";
}
foreach (Item item in childItems)
{
litDataSourceError.Text += "<h2>" + item.Fields["Title"].Value + "</h2>";
}
This is working as expected however these items also have children which I would like to output.
So my question is how to look down a further node within my ForEach to obtain the Childrens Children - there will only be these 2 levels of structure.
You should do the same as you did for your datasource (fetch the children of the Sitecore Item):
foreach (Item item in childItems)
{
litDataSourceError.Text += "<h2>" + item.Fields["Title"].Value + "</h2>";
foreach (Item child in item.GetChildren())
{
...
}
}
I have a treeview panel. In the panel, there are several child nodes. Some of them are only a header.
The way I create the treeview:
treeviewpaneL.Items.Add(art);
art.Items.Add(prt);
some if statement....
TreeViewItem cldhdr = new TreeViewItem() { Header = "ChildNodes:" };
prt.Items.Add(cldhdr);
TreeViewItem cld = new TreeViewItem() .......
........
.....
cldhdr.Items.Add(cld);
Treeview:
Node1
ChildNodes: (This is header only. It appears if child node exists)
Childnode1
Childnode2
childnode3
Node2
Node3
ChildNodes:
Childnode1
Childnode2
childnode3
Node4
Node5
In my treeview there are also images in front of all nodes. It's a code driven treeview. In the xaml part i have only:
<TreeView x:Name="treeviewpaneL" SelectedItemChanged="treeviewpaneL_SelectedItemChanged" >
</TreeView>
What I want to do is when I click on any of the treeview items, I need to get its index number.
My code is:
private void treeviewpaneL_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
int index = 0;
ItemsControl parent = ItemsControl.ItemsControlFromItemContainer(prt);
foreach (var _item in parent.Items)
{
if (_item == treeviewpaneL.SelectedItem)
{
selectedNodeIndex = index;
MessageBox.Show(selectedNodeIndex.ToString());
break;
}
index++;
}
}
With the code above, I can get the index of Node1,Node2,Node3, Node4 and Node5 as 0,1,2,3,4
What I want is to get the index numbers as:
Node1 = 0
Childnode1 = 1 (Skipping the header)
Childnode2 = 2
Childnode3 = 3
Node2 = 4
....
....
....
What am I missing?
Here is the solution, first of all your "MyTreeViewItem"
public class MyTreeViewItem :TreeViewItem
{
private int _index;
public int Index
{
get { return _index; }
set { _index = value; }
}
public MyTreeViewItem() : base()
{
}
}
and usage;
MyTreeViewItem art = new MyTreeViewItem();
art.Header = "Node1";
art.Index = 1;
MyTreeViewItem prt = new MyTreeViewItem();
prt.Header = "Child1";
prt.Index = 2;
art.Items.Add(prt);
treeviewpaneL.Items.Add(art);
and event;
private void treeviewpaneL_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
MyTreeViewItem selectedItem = e.NewValue as MyTreeViewItem;
if (selectedItem != null)
{
MessageBox.Show("" + selectedItem.Index);
}
}
To get the index of the currently selected item:
MyTreeView.Items.IndexOf(MyTreeView.SelectedItem);
I have two method that i'm using to search through a TreeNode in child nodes:
private void FindByText()
{
TreeNodeCollection nodes = treeView1.Nodes;
foreach (TreeNode n in nodes)
{
FindRecursive(n);
}
}
And the FindRecursive:
foreach (TreeNode tn in treeNode.Nodes)
{
string result = Regex.Replace(tn.Text, #"^\d*\.\s*", string.Empty);
if (tn.Text.Contains(this.txtNodeTextSearch.Text))
{
tn.BackColor = Color.Yellow;
tn.EnsureVisible();
count++;
label11.Text = count.ToString();
}
FindRecursive(tn);
}
txtNodeTextSearch is a textBox
The searching is working the problem is that if it found more then one item in any node then this line:
tn.EnsureVisible();
Weill make the treeView1 to automatic scroll down to the last found item. Then i need to scroll manualy up to see the first found items.
Is there anyway to make that it will move to the found items like EnsureVisible() but to the top one first and the one in the bottom ?
I tried to do this:
private void FindRecursive(TreeNode treeNode)
{
for (int i = treeNode.Nodes.Count - 1; i >= 0; i--)
{
TreeNode tn = treeNode.Nodes[i];
string result = Regex.Replace(tn.Text, #"^\d*\.\s*", string.Empty);
if (tn.Text.Contains(this.txtNodeTextSearch.Text))
{
tn.BackColor = Color.Yellow;
tn.EnsureVisible();
count++;
label11.Text = count.ToString();
}
FindRecursive(tn);
}
}
But it didn't change anything still it's scrolling down automatic to the last found item. Or maybe the first found item is in the bottm it dosen't matter i want that it will point/focus on the top found item and then if i want to see the other found items i will scroll down manualy.
i got this tree view and i made it to select all children if parent is checked, the way back as well. There's another rule to check some dependent child too. The problem is: I want to check the parent if any child is checked, but because those other rules i can't find a way to do that without the rules get conflict. So here's is the code i've made until now:
private void tvMorgan_AfterCheck(object sender, TreeViewEventArgs e)
{
//Check Children if parent checked
if (e.Node.Nodes.Count > 0)
{
TreeNode tnParent = e.Node;
if (tnParent.Checked)
{
foreach (TreeNode tnChild in tnParent.Nodes)
{
tnChild.Checked = true;
}
}
//Unchecked children if parent unchecked
else
{
foreach (TreeNode tnChild in tnParent.Nodes)
{
tnChild.Checked = false;
}
}
}
//If dependent node is selected, check the other two
else if (((e.Node.Text.Contains("BRL/EUR")) && (e.Node.Checked)) && (e.Node.Parent.Text.Contains("FWD")))
{
TreeNode tnParent = e.Node.Parent;
foreach (TreeNode tn in tnParent.Nodes)
{
if (tn.Text.Contains("BRL/USD") || tn.Text.Contains("EUR/USD"))
tn.Checked = true;
}
}
//If one of the two necessary nodes are uncheked, then uncheck the dependent one
else if ((((e.Node.Text.Contains("BRL/USD")) || (e.Node.Text.Contains("EUR/USD"))) && (!e.Node.Checked)) && (e.Node.Parent.Text.Contains("FWD")))
{
TreeNode tnParent = e.Node.Parent;
foreach (TreeNode tn in tnParent.Nodes)
{
if (tn.Text.Contains("BRL/EUR"))
tn.Checked = false;
}
}
}
Thanks in advance
The documentation of the TreeView.AfterCheck Event shows exactly how to do what you are looking for
I really can't get out of this one.
I got treeview items in treeviews. The treeview items contain checkboxes with content. How do i get the content and put it in a list.
currently i got this
foreach (TreeViewItem item in treeView1.Items)
{
foreach (TreeViewItem childItem in item.Items)
{
CheckBox checkBoxTemp = childItem.Header as CheckBox;
if (checkBoxTemp == null) continue;
optieListBox.Items.Add(checkBoxTemp.Content);
}
}
I am not sure if i get your question correctly, but you can try this.
foreach (TreeViewItem childItem in item.Items)
{
CheckBox cbx = null;
//finds first checkbox
foreach(object child in childItem.Items){
cbx = child as CheckBox;
if (cbx != null) break;
}
ctrList.Items.Add(cbx.Content);
}
Bind your TreeView to a collection instead. That way you won't have to manipulate UI components to access the data, you will access the data directly.
The other way to do this is through recursion: Declare optieListBox list at class level and call GetContainers() method as an entry point call. optieListBox list should give you content list for all checked items in treeview.
List<string> optieListBox = new List<string>();
private List<TreeViewItem> GetAllItemContainers(TreeViewItem itemsControl)
{
List<TreeViewItem> allItems = new List<TreeViewItem>();
for (int i = 0; i < itemsControl.Items.Count; i++)
{
// try to get the item Container
TreeViewItem childItemContainer = itemsControl.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewItem;
// the item container maybe null if it is still not generated from the runtime
if (childItemContainer != null)
{
allItems.Add(childItemContainer);
List<TreeViewItem> childItems = GetAllItemContainers(childItemContainer);
foreach (TreeViewItem childItem in childItems)
{
CheckBox checkBoxTemp = childItem.Header as CheckBox;
if (checkBoxTemp != null)
optieListBox.Items.Add(checkBoxTemp.Content);
allItems.Add(childItem);
}
}
}
return allItems;
}
private void GetContainers()
{
// gets all nodes from the TreeView
List<TreeViewItem> allTreeContainers = GetAllItemContainers(this.objTreeView);
// gets all nodes (recursively) for the first node
TreeViewItem firstNode = this.objTreeView.ItemContainerGenerator.ContainerFromIndex(0) as TreeViewItem;
if (firstNode != null)
{
List<TreeViewItem> firstNodeContainers = GetAllItemContainers(firstNode);
}
}
Try this:
List<string> values = new List<string>;
foreach (string node in treeView.Nodes)
{
values.Add(node);
}
//Loop through nodes
Also, if the tree view's nodes have children (nodes), try this instead:
List<string> values = new List<string>;
//Called by a button click or another control
private void getTreeValues(Object sender, EventArgs e)
{
foreach (string node in treeView.Nodes)
{
TreeNode child = (TreeNode)child;
values.Add(node)
getNodeValues(child);
}
foreach (string value in values)
{
Console.WriteLine(value + "\n");
}
}
//Recursive method which finds all children of parent node.
private void getNodeValues(TreeNode parent)
{
foreach (string child in parent.Nodes)
{
TreeNode node = (TreeNode)child;
values.Add(child);
if (nodes.Nodes.Count != 0) getNodeValues(child);
}
}