Search code examples
wpfxamluwpdatatemplatewinui-3

Change Styling for WinUI3 TreeView Items On State


I am trying to get a TreeView to look kind of "grayed out" and disable the use of it on a boolean being true. The issue I am having is the current layout of the XAML seems to be preventing this and I am having a hard time finding a way around it.

This is the layout of the XAML I'm using and am not sure how/if I can pass the Parent's DataContext down into the DataTemplate:

<Grid>
 <TreeView>
  <TreeView.ItemTemplate>
   <DataTemplate x:DataType="ViewModelObject">
    <TreeViewItem ItemSource="{x:Bind Children" Content="{x:Bind Name}"/>
   </DataTemplate>
  </TreeView.ItemTemplate>
 </TreeView>
</Grid>

What I would like to do is to add a field of Foreground to the TreeViewItem to change the color based off a variable in the ViewModel, but the DataTemplate having a separate DataContext is preventing this.

If there are other ways to make it have that "grayed out" disabled look, I would also love advice there, this is just the best way I came up with.


Solution

  • The easiest way is to use {Binding} instead of {x:Bind}, that will let you use the ElementName property to get the page (give it an x:Name first) and from there you can navigate to your ViewModel property and to whatever you need out of it.