In Xamarin Forms, I have a custom view with a BindableProperty:
public static readonly BindableProperty SelectedItemProperty
= BindableProperty.Create(nameof(SelectedItem), typeof(object), typeof(OneOfNButtons), null,
BindingMode.TwoWay);
I use that custom view within a custom page:
<exodus:OneOfNButtons ItemsSource="{Binding Tabs}" SelectedItem="{Binding SelectedTab, Mode=TwoWay}"/>
The two-way binding works as expected.
Now I want to trigger an animation that moves the entire OneOfNButtons control up to middle of page, so I can show related content below it, whenever SelectedItem
changes. That is, it is not the primary focus of the page, it is down at bottom. If user is interested in it, they click any of the buttons on it, and half the page becomes dedicated to that topic, with the row of buttons acting like tabs, just above the content.
I understand DataTriggers
, but those are for specific values. I'm looking for a trigger on any change to SelectedItem
property. (If the logic belonged within the custom control itself, I could add code to the SelectedItem setter.)
This is logic specific to the page, so it belongs with the page; is not part of the custom control.
This is logic specific to this UI, so it belongs with the page, not with page's view model.
I've written the question as "invoke a code behind method", because that is the technique I would like to know how to do in general, even if this specific situation could be handled entirely in XAML.
Though I would also be glad to know how to trigger other XAML, on any change.
(If you know a WPF XAML technique, it might work in Xamarin Forms, though XAML here is more limited. Specifically, X-Forms bound properties are not "DependencyProperty"s, so I don't know how to "chain" properties.)
There are a number of XAML & data binding questions on SO, but all the ones I have found either discuss binding between view and model, or within a single view (I need a change in one view to affect the containing view), or involve messaging between the views (the sub-view does not and should not know about this requirement, so messaging isn't applicable), or triggering on specific values, or are for WPF and don't appear to be supported in Xamarin Forms.
You can handle any property changed events with this.
this.YourControl.PropertyChanged += YourControlPropertyChanged;
private void YourControlPropertyChanged(object sender,PropertyChangedEventArgs e)
{
if(e.PropertyName == "SelectedItem")
{
//Code
}
}