/// <summary> /// Update the selected value of the of the TreeView based on the value /// of the currently selected TreeViewItem and the SelectedValuePath. /// </summary> /// <param name="item"> /// Value of the currently selected TreeViewItem. /// </param> private void UpdateSelectedValue(object item) { if (item != null) { string path = SelectedValuePath; if (string.IsNullOrEmpty(path)) { SelectedValue = item; } else { // Since we don't have the ability to evaluate a // BindingExpression, we'll just create a new temporary // control to bind the value to which we can then copy out Binding binding = new Binding(path) { Source = item }; ContentControl temp = new ContentControl(); temp.SetBinding(ContentControl.ContentProperty, binding); SelectedValue = temp.Content; // Remove the Binding once we have the value (this is // especially important if the value is a UIElement because // it should not exist in the visual tree once we've // finished) temp.ClearValue(ContentControl.ContentProperty); } } else { ClearValue(SelectedValueProperty); } }