0

Starting off with a classic datatemplate:

<DataTemplate x:Key="RegularTemplate">

   <Grid>
     ...
   </Grid>

</DataTemplate>

assume the ViewModel object that is being rendered using the above template has the following property:

    private Visibility _Visibility;
    public Visibility VMVisibility
    {
        set
        {
            if (value == _Visibility) return;
            _Visibility = value;
            if (value == System.Windows.Visibility.Visible)
            {
                ViewRefresher.TwentySecondsTick += Tick;
            }
            else
            {
                ViewRefresher.TwentySecondsTick -= Tick;
            }
        }

        private get
        {
            return _Visibility;
        }
    }

I want that setter code to somehow run when the listboxitem is not rendered by the panel containing it. It's a custom panel that hides/shows items during scrolling, so I just need to bind to the listboxitems's visibility somehow.

I've tried stuff along the lines of:

<DataTemplate x:Key="RegularTemplate">

    <Grid>
     ...
    </Grid>

    <DataTrigger Binding="{Binding IsVisible,RelativeSource=
                      {RelativeSource FindAncestor,  
                      AncestorType={x:Type ListBoxItem}}}" Value="False">
         <Setter  Property="{Binding VMVisibility}" Value="False"/>
     </DataTrigger>

</DataTemplate>

but you can't use datatriggers that way.

Any ideas?

Jamona Mican
  • 1,574
  • 1
  • 23
  • 54

1 Answers1

0

Don't you hate it when you spend 2 hours searching for a solution, give up, post on stackoverflow, only to find it 5 minutes later?

Answer:

Using this guy's behaviour: https://stackoverflow.com/a/3667609/855551

You can bind the isVisible of the parent to your viewmodel:

<DataTemplate x:Key="RegularTemplate">

<Grid>
<behaviour:DataPiping.DataPipes>
        <behaviour:DataPipeCollection>
              <behaviour:DataPipe Source="{Binding RelativeSource=
                   {RelativeSource AncestorType={x:Type ListBoxItem}}, 
                         Path=IsVisible}"
                     Target="{Binding Path=Visible, Mode=OneWayToSource}"/>
              </behaviour:DataPipeCollection>
        </behaviour:DataPiping.DataPipes>
 ...
</Grid>


</DataTemplate>

VM Object:

    private bool _Visible;
    public bool Visible
    {
        set
        {
            if (value == _Visible) return;
            _Visible = value;
            if (value == true)
                EnableRefresh();
            else
                DisableRefresh();
        }

        private get
        {
            return _Visible;
        }
    }
Community
  • 1
  • 1
Jamona Mican
  • 1,574
  • 1
  • 23
  • 54