I have a flipView which has a Grid inside. Is there any way to find the grid as child of flipView item?
I have tried using FlipView.FindChild(). But it returns null value. This is the sample xaml code,
<FlipView>
<FlipView.ItemTemplate>
<DataTemplate x:DataType = "data:SampleData">
<Grid x:Name="SampleGrid">
===Sample Code====
</Grid>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
For your scenario, it is recommended to use VisualTreeHelper to find the button in the Grid.
<Grid>
<FlipView ItemsSource="{x:Bind Items, Mode=OneWay}">
<FlipView.ItemTemplate>
<DataTemplate>
<Grid PointerEntered="Grid_PointerEntered" PointerExited="Grid_PointerExited">
<MediaElement Source="{Binding mediaSource}" AutoPlay="True" />
<Button x:Name="btnTest" Content="Test" Background="Yellow" Click="Button_Click" VerticalAlignment="Top" Visibility="Collapsed"></Button>
<Border Background="#A5000000" Height="80" VerticalAlignment="Bottom">
<TextBlock Text="{Binding Name}" FontFamily="Segoe UI" FontSize="26.667" Foreground="#CCFFFFFF" Padding="15,20" />
</Border>
</Grid>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
</Grid>
public sealed partial class MainPage : Page
{
public ObservableCollection<FlipViewItem> Items { get; } = new ObservableCollection<FlipViewItem>();
public MainPage()
{
this.InitializeComponent();
LoadData(); // Load your data here (e.g., from a service or local storage)
}
private void LoadData()
{
// Example data (replace with your actual data)
Items.Add(new FlipViewItem { Name = "Test1", mediaSource = "ms-appx:///Assets/test1.mp4" });
Items.Add(new FlipViewItem { Name = "Test2", mediaSource = "ms-appx:///Assets/test2.mp4" });
Items.Add(new FlipViewItem { Name = "Test3", mediaSource = "ms-appx:///Assets/test3.mp4" });
Items.Add(new FlipViewItem { Name = "Test4", mediaSource = "ms-appx:///Assets/test4.mp4" });
// Add more items as needed
}
private void Button_Click(object sender, RoutedEventArgs e)
{
}
private void Grid_PointerEntered(object sender, PointerRoutedEventArgs e)
{
Grid grid = (Grid)sender;
Button btnTest = FindChild<Button>(grid);
if (btnTest != null)
{
// Do something with the found TextBox
btnTest.Visibility = Visibility.Visible;
}
}
private void Grid_PointerExited(object sender, PointerRoutedEventArgs e)
{
Grid grid = (Grid)sender;
Button btnTest = FindChild<Button>(grid);
if (btnTest != null)
{
// Do something with the found TextBox
btnTest.Visibility = Visibility.Collapsed;
}
}
public static T FindChild<T>(DependencyObject parent) where T : DependencyObject
{
if (parent == null)
return null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is T typedChild)
return typedChild;
// Recursively search deeper into nested containers
T result = FindChild<T>(child);
if (result != null)
return result;
}
return null; // Child not found
}
public class FlipViewItem
{
public string Name { get; set; }
public string mediaSource { get; set; }
}
}
public static T FindChild<T>(DependencyObject parent) where T : DependencyObject
{
if (parent == null)
return null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is T typedChild)
return typedChild;
// Recursively search deeper into nested containers
T result = FindChild<T>(child);
if (result != null)
return result;
}
return null; // Child not found
}
public static void FindChildList<T>(List<T> results, DependencyObject startNode) where T : DependencyObject
{
int count = VisualTreeHelper.GetChildrenCount(startNode);
for (int i = 0; i < count; i++)
{
DependencyObject current = VisualTreeHelper.GetChild(startNode, i);
if (current is T typedChild)
{
results.Add(typedChild);
}
FindChildList(results, current);
}
}
private void FlipView_PointerEntered(object sender, PointerRoutedEventArgs e)
{
FlipView flipView = (FlipView)sender;
List<Grid> grids= new List<Grid>();
FindChildList<Grid>(grids,flipView);
for (int i = 0; i < grids.Count; i++)
{
Grid grid = grids[i];
Button btnTest = FindChild<Button>(grid);
if (btnTest != null)
{
btnTest.Visibility = Visibility.Visible;
}
}
}
private void FlipView_PointerExited(object sender, PointerRoutedEventArgs e)
{
FlipView flipView = (FlipView)sender;
List<Grid> grids = new List<Grid>();
FindChildList<Grid>(grids, flipView);
for (int i = 0; i < grids.Count; i++)
{
Grid grid = grids[i];
Button btnTest = FindChild<Button>(grid);
if (btnTest != null)
{
btnTest.Visibility = Visibility.Collapsed;
}
}
}