Adding button on flipview in UWP

42 Views Asked by At

Update:

Solved the problem by inserting a grid with two rows in FlipView control. Than added media element spanning two rows and buttons in second row in another grid. Similar to this,

<FlipView>
    <Grid PointerEntered ="PointerEntered_Event">
        <MediaElement ...../>
        <Button />
    </Grid>
</FlipView>

Then in the code behind,

    private void PointerEntered_Event(object sender, PointerRoutedEventArgs e)
{
    var temp  = sender as grid;
    temp.children[1].Visibility = Visibility.Visible;
}

/////////////Update End//////////////////////

I am trying to add a button in flipview which has multiple sample videos. The button should show during mouse hover event. But I could not find any way to show the button during mouse hover event. Is this even possible?

I am trying to add a button in flipview which has multiple sample videos. The button should show during mouse hover event. But I could not find any way to show the button during mouse hover event. Is this even possible?

I have tried to capture mouse hover event than create a button but that failed. This is my flipview implementation,

<FlipView x:Name="SampleFlipView" CornerRadius="10"
      Margin="0,5,0,0"
      Width="100"
      Height="200"
      ItemsSource="{x:Bind ViewModel.SampleItems, Mode=OneWay}">

    <FlipView.ItemTemplate>
        <DataTemplate x:DataType="data:SampleItem">
            <MediaElement Source ="{x:Bind ItemSource}" AutoPlay="True" IsLooping="True"/>
        </DataTemplate>
    </FlipView.ItemTemplate>
</FlipView>
2

There are 2 best solutions below

3
Junjie Zhu - MSFT On BEST ANSWER

It is recommended to use PointerEntered event to handle mouse hover and use Visibility to show and hide button.

<MediaElement  Source="Assets\test.mp4" AutoPlay="True" IsLooping="True" 
               PointerEntered="MediaElement_PointerEntered" PointerExited="MediaElement_PointerExited"/>
<Button x:Name="btnTest" Content="Test"  Background="Yellow" Click="Button_Click" VerticalAlignment="Top" Visibility="Collapsed"></Button>

 private void MediaElement_PointerEntered(object sender, PointerRoutedEventArgs e)
 {
     btnTest.Visibility = Visibility.Visible;
 }

 private void MediaElement_PointerExited(object sender, PointerRoutedEventArgs e)
 {
     btnTest.Visibility = Visibility.Collapsed;
 }

Update

I noticed your new question and understood your scenario, here is my complete test code. 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; }


     }
 }
0
Sumnoon On

Answering my own question

Solved the problem by inserting a grid with two rows in FlipView control. Than added media element spanning two rows and buttons in second row in another grid. Similar to this,

<FlipView>
    <Grid PointerEntered ="PointerEntered_Event">
        <MediaElement ...../>
        <Button />
    </Grid>
</FlipView>

Then in the code behind,

    private void PointerEntered_Event(object sender, PointerRoutedEventArgs e)
{
    var temp  = sender as grid;
    temp.children[1].Visibility = Visibility.Visible;
}