I Have Treeview (shown as above) in my app, I have binded it with collection...
now the problem is whenever I expand on Colorodo by default Aspen should get selected, means whenever I expand first item that Node should get selected..
Any Ideas/suggestion Please
Unfortunately, as I'm sure you have discovered, is that you cannot set the treeViewInstance.SelectedItem property as it's read-only.
From memory, each TreeViewItem has a IsSelected property that you can set. You try to listen for expand/collapse on the items and maybe set this property. Without trying this myself I don't know if it is a) a good solution b) if it would even works.
To get the TreeViewItem that is the container for the item in collection use
treeViewInstance.ItemContainerGenerator.ContainerFromItem(yourDataItem) as TreeViewItem;
Another idea (the way I would do it) is to use a ListBox/ListView and fake the hierarchical view. The create a view model controller and item, wrap your data, and manage this all yourself. If you want more information, please leave a comment and I will dig up a few examples to help.
HTH,
Dennis
There are two apporches choose what ever you like.
i) explore ItemTemplageSelector, not sure but may be possible to do work with it.
Other wise on tree expand event or mouse capture event get the Current Root node and then select its first child with index like rootNode.child[0]... set this is as Selected True or Isfocused and perfom operation that is intended on its click you you will make user feel like it is selected.
Related
I have a treeview that displays a few nodes, say 5 or so. When the final node is selected, I want to display records belonging to that final node (linked via a foreign key) inside of a listbox.
So the structure would be
Treeview Listbox
-1 -Object belonging to 5
--2 -Object belonging to 5
---3
----4
-----5
My question is if such a comperation between these two controls is even possible and if I'm going about it the smartest way.
I can't find anything about it (getting actual data from the last selected node in the treeview is already pretty hard on how to find a how to). Any tips in the right direction would be very appreciated.
It is possible to do this exactly the way you're trying to by using attached properties, but it's a bit of a clumsy way of going about it. What you really should be doing is using data binding.
Your TreeView is, presumably, bound to some kind of data structure in your view model (data context), and what class that is should (again, presumably) be able to easily ascertain whether or not a particular given item is the deepest/last one in the tree. So create a property in that class and bind the TreeView's SelectedItem to that, so that it gets updated whenever the user selects an item in the Tree. Next, create another property for your ListBox to bind to. When your first property gets set, it either sets this second property to the currently selected item if it's the last in the list, or sets it to null if it isn't.
By doing this you de-couple your logic from your view and you make something that's much easier to debug, test and modify in future.
My Search Contract populates a SearchContractResultsPages resultsList with objects, however when I try to select an item (which opens other page and passes the selection as a navigation parameter), the first item is always selected and passed as the parameter. I simply have no idea what to do to fix this, or what code needs changing, the resultsListView.SelectedItem is always the item opened, no matter what I actually select. The selection logic is carried out on the DoubleTapped event.
I don't know what code to post, so if anyone has any ideas, I will happily post I relevant parts you need to see.
Bit of a cop-out before I answer, but it depends...
If you are navigating to another page on SelectedItemChanged, the selected item should probably already be the correct one and you can use that as your navigation parameter. If this isn't working we will need to see more (come) code.
If you have an ItemClick event handler on the list view, you should use e.ClickedItem to get the correct reference back. Again, if this is what you are doing and it isn't working, we will need to see code.
Ah, I see you are switching selections on Double tapping the item. This could be the cause of your problem. Double-clicking to select is not one of the usual ways to select an item in a list. You should probably stick with the "small swipe" to select, as virtually all other apps in the ecosystem do. Not only will this let you verify what selection you have made before doing anything with it, but it will not confuse users since they're already used to the paradigm.
A stackpanel's dragOver event gives me access to the stackpanel as sender, but how can I get the name of the listbox that the stackpanel is in (stack panel is defined in the datatemplate). The reason for this is to ultimately access all of the other stackpanels in the list generated by the datatemplate so if I'm barking up the wrong tree here I'll appreciate an alternative way of accessing the other stackpanels. Thanks,
Dan.
Updated (working) answer
You need to walk up the visual tree with VisualTreeHelper.GetParent on the StackPanel, and if necessary recurse over further parents, until you find the ListBox (use the as or is operator to detect that you have found it).
For added convenience, or if you need to traverse the tree in other directions (which is a bit more involved), you can use a wrapper that exposes the traversal in a LINQy format such as this one.
All this said, you might also want to take a look at How can I find WPF controls by name or type?.
Use the .Parent property to get the object that the current object is hosted in.
var listboxName = (stackpanel.Parent as ListBox).Name;
According to this the StackPanel will not reach the root of the logical tree by the Parent property as it is located inside a template, so use the TemplatedParent property. In it you will probably get a ListBoxItem, which will have listbox as its parent.
Within my winform(C#) I have listView with two columns, which is used to display the results from searching an array, which is the basic details of an array entry(All data within the array is type string). I am able to add and remove items to the listview, but I would like to be able to gain the value of the row that has been clicked within the listview.
I am planning to use this to update the display full entry info, but I cannot gain the value of the selected item. I was reading elsewhere and saw people mentioning the .SelectedIndex property, but this property is not available to me when I am trying to code.
I could use two listboxes instead of a listview, but the listview is much neater. Also, I am a sofware design (high school) student, and have been learning C# for one and a half years. I am competent at programming, but I start to get lost when things start to get very complex.
Can anybody help?
The ListView.SelectedItems property works fine if the ListView is not in VirtualData mode.
you should use the SelectedItem property and Tag property
var item = (MyClass) listView1.SelectedItems[0].Tag;
tag property allow you to set any type for example MyClass
when you populate the ListView set the Tag property.
For ListView check SelectedItems
You could trap the SelectedIndexChanged event and in that you could do
ListView.SelectedListViewItemCollection listItems=
this.myListView.SelectedItems;
foreach ( ListViewItem item in listItems)
{
MessageBox.Show(item.SubItems[0].Text);
MessageBox.Show(item.SubItems[1].Text);
}
I have fixed the problem. After talking to a friend in my software class, he told me to use listView1.FocusedItem.Index, which works perfectly for my needs, as it gains the index of the selected item. Also, thank you to everyone for their input in trying to help me.
Apologies for not posting any code for this but all my efforts seem to be going nowhere.
I need to get the name of an observableCollection a textblock is bound to from the mouseDown event handler, so I can then perform some operations on the data, is there any way to do this?
I have set the Tag of the textblock to {binding} so i get the entire object back the textblock is bound to. Other than this im not sure where to go next.
Update:
The reason for this is that I have 2 multiselect treeviews, with a heirarchial data template, each template shares the same treeview but is bound to a different observable collection.
The way my multiselect works is by applying a style to each treeview if the IsSelected value of that item in my collection is true.
Now I am using the Mousedown event handler for the textblock in my datatemplate to get the item I am working on, BUT some items can be in both collections at once. I need to know which item to set the IsSelected value on. Using Binding{tag} I get the Item I need to set on but not the collection it is within.
I'm using Mousedown as the event handler because If you click on one item to select it, then click again it needs to unselect and the treeview event handlers didnt seem to allow this to happen (SelectedItemChanged etc).
As a side note I also need to be able to hide the default selected style of the Treeview as this isnt used and it gets confusing.
You can't determine what collection an item is in, but you can determine what TreeView the user clicked on. Then you can get the collection by knowing the TreeView, which should solve your immediate problem.