Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i call a function via DataTrigger (XAML)?

I have a function in .xaml.cs which i wanted to call:

.xaml.cs

private void treeView_SelectedItemChanged(object sender, EventArgs e)
{
        //treeView.ScrollToCenterOfView(sender as TreeViewItem);
}

like from this post: Make ListView.ScrollIntoView Scroll the Item into the Center of the ListView (C#) Answer 1

But i dont use the SelectedItem, my approach would be like:

XAML:

<Style.Triggers>
     <DataTrigger Binding="{Binding Path=IsActive}" Value="true">
          // ###call this function
     </DataTrigger>
</Style.Triggers>

I dont have any Idea how to raise this function by change of the DataModel. In every Tutorial i have gooled they do it via Event/Command.

Thx

like image 463
Michael Spannbauer Avatar asked Oct 16 '25 15:10

Michael Spannbauer


1 Answers

Just to add another slightly hacky option for these scenarios.

Not quite a Trigger, but you can utilise the generic FrameworkElement.Tag property to set up a binding for the property you want to watch, and then run a callback when it changes.

eg:

<DataTemplate>
    <StackPanel Tag="{Binding IsActive, NotifyOnTargetUpdated=True}"
                TargetUpdated="CodeBehindMethodToCall">

        <!-- snip rest of template --> 

    </StackPanel>
<DataTemplate>
like image 195
GazTheDestroyer Avatar answered Oct 18 '25 03:10

GazTheDestroyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!