Reputation: 6667
I have a FlipView with some controls in the DataTemplate. Since it's in a FlipView, it will generate multiple copies of those controls. If I want to find the control that is in the current FlipView view (i.e. on screen / visible), how can I do it?
I can check the loaded event on the control inside but it will be called multiple times and I won't know which one is being shown.
Upvotes: 0
Views: 1271
Reputation: 6091
Create an Attached Dependency Property that you can bind to the RichTextBlock from the ViewModel, such as:
public static class MyStaticClass
{
public static readonly DependencyProperty IsVisible = DependencyProperty.RegisterAttached("IsVisible", typeof(bool), typeof(MyStaticClass), new PropertyMetadata(false, OnVisibilityChanged));
private static void OnVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var rtb = (RichTextBlock)d;
var isVisible = (bool)e.NewValue;
// Do something to rtb.Inlines
}
}
With this property, you can bind it to the ViewModel's IsSelected property:
<FlipView ItemsSource="{Binding SomeList}" SelectedItem="{Binding SelectedVM, Mode=TwoWay}">
<FlipView.ItemTemplate>
<DataTemplate>
<RichTextBlock ns:MyStaticClass.IsVisible="{Binding IsSelected}" />
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
When the SelectedItem is changed, you can set IsSelected on the child View Model to true, and that triggers the MyStaticClass.OnVisibilityChanged event.
Upvotes: 1