0

How might I do THIS: How to make WPF TreeView style as WinForms TreeView? in code, with no XAML? The reason I need to do this is the TreeView I have been given is defined in code, so there is no XAML at all. While I can start with XAML, I do not know how to 'add XAML' to something that doesn't have it to begin with. I am very new to WPF, but have been doing Winforms for years. The TreeView is comprised only of a large hierarchy of TreeViewItem objects with Header and Tag values.

Cœur
  • 37,241
  • 25
  • 195
  • 267
MikeGeek
  • 21
  • 7
  • What do you mean your treeview is "defined in code"? If there are big UI customizations, that is likely impossible. Unless the original developer had a death wish. My guess is that you are uncertain how it's put together? Please edit your question to show an example of what you're talking about. – SledgeHammer Dec 12 '18 at 04:16

1 Answers1

0

If you look at the answer to the article you linked to, it details a TreeViewItem style defined like this:-

<Style x:Key="{x:Type TreeViewItem}" TargetType="{x:Type TreeViewItem}">

When the x:Key is set to the target type (or omitted entirely), the style should become the application default for that control type.

If you add the styles detailed in that article's answer to your App.xaml file, all TreeViewItems throughout your application should take on this default style.

If this doesn't work, you can try one more thing. Change the style's key to a string, e.g.:-

<Style x:Key="WindowsTreeViewItemStyle" TargetType="{x:Type TreeViewItem}">

Then in your code, iterate through the TreeViewItems in the TreeView, and assign the style to each one programmatically:-

var style = (Style)Application.Current.FindResource("WindowsTreeViewItemStyle");

foreach (var treeViewItem in ...)
{
   treeviewitem.Style = style;
}

(Use that article's XAML at your own risk - the link to the complete source zip is dead, and what's detailed in his SO answer may be incomplete!)

Andrew Stephens
  • 9,413
  • 6
  • 76
  • 152
  • I don't have an App.xaml file. What I have is a large app with 'hooks' where I can specify assemblies and classes for customization. I have been able to customize it with pure code. I don't know (or understand) how to force the connection between XAML and code as I don't actually understand how that connection 'naturally' occurs. In your answer, I don't know where I'd put the XAML if I don't have access to the App.xaml. What I have is a set of compiled .Net assemblies and some of them are my own that get loaded by the baseline application. – MikeGeek Sep 14 '16 at 13:53