C#實現win10 uwp 右擊浮出窗在點擊位置
本文主要讓MenuFlyout出現在我們右擊位置。
我們一般使用的MenuFlyout寫在前臺,寫在Button里面,但是可能我們的MenuFlyout顯示的位置和我們想要的不一樣。
通過使用后臺寫ShowAt的方法,我們可以通過e.GetPosition獲得鼠標點擊位置,需要對函數傳入相對的元素,這個元素一般可以用我們點擊使用的元素,也可以使用我們的最外層Grid,這樣我們就可以獲得了鼠標位置,也就可以顯示我們的MenuFlyout在點擊位置。
我們建一個ListView,然后綁定后臺,在我們ListView要右擊顯示我們的浮出,要求我們的浮出在我們點擊位置。
MenuFlyout可以在后臺寫,當然寫在前臺也可以。
我們這寫在后臺,我們可以選擇Placement 顯示在我們元素的位置,但這不是我們鼠標點擊的位置,要顯示我們鼠標點擊的位置,其實也很簡單。我們可以從e.GetPosition(sender as UIElement)獲得鼠標位置,把這個給MenuFlyout我們的浮出顯示在我們鼠標點擊位置
<ListView ItemsSource="{x:Bind View.Str}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Center"></Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Grid Background="#FFda2a5c" RightTapped="GridColection_OnRightTapped">
<TextBlock Text="{Binding}"></TextBlock>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
后臺寫
private void GridColection_OnRightTapped(object sender, RightTappedRoutedEventArgs e)
{
MenuFlyout myFlyout = new MenuFlyout();
MenuFlyoutItem firstItem = new MenuFlyoutItem { Text = "OneIt" };
MenuFlyoutItem secondItem = new MenuFlyoutItem { Text = "TwoIt" };
myFlyout.Items.Add(firstItem);
myFlyout.Items.Add(secondItem);
//if you only want to show in left or buttom
//myFlyout.Placement = FlyoutPlacementMode.Left;
FrameworkElement senderElement = sender as FrameworkElement;
//the code can show the flyout in your mouse click
myFlyout.ShowAt(sender as UIElement, e.GetPosition(sender as UIElement));
}

以上所述是小編給大家介紹的C#實現win10 uwp 右擊浮出窗在點擊位置,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
C#使用foreach遍歷哈希表(hashtable)的方法
這篇文章主要介紹了C#使用foreach遍歷哈希表(hashtable)的方法,是C#中foreach語句遍歷散列表的典型應用,非常具有實用價值,需要的朋友可以參考下2015-04-04

