简体   繁体   English

有没有办法在 WinUI3 中更改弹出窗口的父内容?

[英]Is there a way to change the parent content for a popup in WinUI3?

In WinUI3 it seems that you are unable to modify the duration of a tooltip (similar to how WPF allowed a duration change with the ToolTipService.ShowDuration ).在 WinUI3 中,您似乎无法修改工具提示的持续时间(类似于 WPF 如何允许使用ToolTipService.ShowDuration更改持续时间)。

As such, I'm attempting to replicate the behavior using a Popup control such that the Popup will remain visible as long as the mouse is hovered over an element (utilizing the element's OnPointerEntered / OnPointerExited events to modify the IsOpen property of the Popup).因此,我尝试使用 Popup 控件复制行为,以便只要鼠标悬停在元素上 Popup 就会保持可见(利用元素的OnPointerEntered / OnPointerExited事件修改 Popup 的IsOpen属性)。

This displays the Popup correctly, however in my case the Popup exists inside a Grid which is inside the ListView.ItemTemplate for a ListView .这会正确显示 Popup,但在我的例子中,Popup 存在于Grid内,而 Grid 位于ListView.ItemTemplateListView内。 ala:翼:

<ListView>
  <DataTemplate>
    <Grid>
      <TextBlock/>
      <Popup/>
    </Grid>
  </DataTemplate>
</ListView>

Thus the popup only overlays the <Grid> , but still gets clipped within the region of the DataTemplate .因此,弹出窗口仅覆盖<Grid> ,但仍会在DataTemplate的区域内被裁剪。

I'd like to have the popup overlay the <ListView> itself instead - is there a way to specify which content the popup will overlay such that it will overlay the <ListView> ?我想让弹出窗口覆盖<ListView>本身 - 有没有办法指定弹出窗口将覆盖哪些内容,以便它覆盖<ListView>

Or even better - to have it overlay all elements of the Window (replicating the placement of a tooltip?)或者甚至更好 - 让它覆盖 Window 的所有元素(复制工具提示的位置?)

Is there a way to change the parent content for a popup in WinUI3?有没有办法在 WinUI3 中更改弹出窗口的父内容?

Sure, you could place Popup at same level as ListView in current page.当然,您可以将 Popup 放置在与当前页面中的ListView相同的级别。 And listen ListView item PointerEntered and PointerExited event then pass current item datacontext to popup control.并监听 ListView 项目PointerEnteredPointerExited事件,然后将当前项目数据上下文传递给弹出控件。

Xaml Xaml

<ListView
    Margin="0,10,0,45"
    ItemsSource="{x:Bind Items}"
    Visibility="Visible">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel
                Orientation="Vertical"
                PointerEntered="StackPanel_PointerEntered"
                PointerExited="StackPanel_PointerExited">
                <TextBlock x:Name="Id" Text="{Binding ID}" />
                <TextBox
                    x:Name="FirstName"
                    GettingFocus="FirstName_GettingFocus"
                    Text="{Binding FirstName}" />
                <TextBox x:Name="LastName" Text="{Binding LastName}" />
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
<Popup
    x:Name="PopupTip"
    HorizontalAlignment="Center"
    VerticalAlignment="Center">
    <Popup.ChildTransitions>
        <TransitionCollection>
            <PaneThemeTransition Edge="Bottom" />
        </TransitionCollection>
    </Popup.ChildTransitions>

    <StackPanel
        Width="150"
        Height="80"
        Background="LightGray"
        CornerRadius="4">
        <TextBlock
            x:Name="InfoLabel"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Text="{Binding ID}" />
    </StackPanel>
</Popup>

Code behind代码隐藏

private void StackPanel_PointerEntered(object sender, PointerRoutedEventArgs e)
{
    var ItemDataContext = (sender as FrameworkElement).DataContext;
    PopupTip.DataContext = ItemDataContext;
    PopupTip.IsOpen = true;
  
}

private void StackPanel_PointerExited(object sender, PointerRoutedEventArgs e)
{
    PopupTip.DataContext = null;
    PopupTip.IsOpen = false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM