Search code examples
wpfdata-bindingadorner

WPF Adorner Visibility Data Binding programmatically


I'm creating a Loading Adorner that has a swirling icon over it. I tried binding the visibility property directly in the XAML but that actually hides everything inside its hierarchy.

I have this in my XAML:

<AdornerDecorator Visibility="{Binding Path=RootGroup.Loading, Converter={StaticResource VisibilityConverter}}">
    <TreeView x:Name="groupTreeView" />
</AdornerDecorator>

and this in my constructor

LoadingAdorner adorner = new LoadingAdorner(groupTreeView);
AdornerLayer.GetAdornerLayer(groupTreeView).Add(adorner);

This isn't want I wanted so I tried binding it in the code instead:

LoadingAdorner adorner = new LoadingAdorner(groupTreeView);
Binding bind = new Binding("RootGroup.Loading");
bind.Source = this.DataContext;
bind.Converter = new VisibilityConverter();
adorner.SetBinding(LoadingAdorner.VisibilityProperty, bind);
AdornerLayer.GetAdornerLayer(groupTreeView).Add(adorner);

This will work if the DataContext is not null because it can actually find RootGroup.Loading. But if it is null then the binding has no source to look at.

So I was wondering what does the XAML databinding use as its .Source ? Binding directly in the XAML binds to the correct property, but it doesn't achieve the same result. So I'm just wondering what I should be setting my .Source to So i can bind to RootGroup.Loading ?

Thanks, Raul


Solution

  • This doesn't directly answer your question, but why are you using an adorner to get the loading animation effect.

    Why not just use a border element that is a sibling of your TreeView that is Z-Ordered on top and then do your animation in that.

    So you do something like this

    <Grid>    
      <TreeView />
      <Border x:Name="myBorder">... </Border>      
    </Grid>
    

    Then you can do all your binding in XAML without hiding the entire Visual Tree.