WPF: Programmatically select item in Treeview

  • Thread starter Thread starter Thorsten Tarrach
  • Start date Start date
T

Thorsten Tarrach

Hi,

unfortunately there does not seem to be a managed Newsgroup for WPF, so I
posted my question here:

In a databound Treeview I cannot access the TreeViewItems directly and the
SelectedItem property is real-only.
I can also not change the databound items it the backend to contain a
Selected property. This is because I bind against XML.

So, is there a simple way to programmatically traverse the TreeView and
select a specific item?

Thanks, Thorsten
 
Hello Thorsten,

Thanks for using Microsoft Newsgroup Support Service, my name is Colbert
Zhou [MSFT] and I will be working on this issue with you.

We can use the ItemContainerGenerator.ContainerFromItem() to get the item's
UI TreeViewItem instance, and then we can use Reflection to call its Select
method. The following codes work fine in my side,
private void button1_Click(object sender, RoutedEventArgs e)
{
DependencyObject dObject =
this.treeView1.ItemContainerGenerator.ContainerFromItem(this.treeView1.Items
[1]);
((TreeViewItem)dObject).IsSelected = true;
MethodInfo selectMethod =
typeof(TreeViewItem).GetMethod("Select",BindingFlags.NonPublic |
BindingFlags.Instance);
selectMethod.Invoke(dObject, new object[] { true });
}

For detailed information, please see the following blog article which does
explain this topic,
http://askernest.com/archive/2008/01/23/how-to-programmatically-change-the-s
electeditem-in-a-wpf-treeview.aspx

Please let me know if this addresses your question. Any future assistance
we can provide, please feel free to contact me. Have a good day!


Best regards,
Colbert Zhou (colbertz @online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top