【问题标题】:Popup control moves with parent弹出控件随父级移动
【发布时间】:2015-02-01 15:07:23
【问题描述】:

希望这对您在这里的 WPF 专家来说是一个快速的。当寡妇移动或改变大小时,是否有任何简单的 XAML 解决方案允许我的弹出窗口移动?代码如下。如果没有,我总是可以在后面的代码中处理一个事件。

<Grid>
    <Canvas>
        <Expander Header="details" HorizontalAlignment="Center" VerticalAlignment="Top" ExpandDirection="Down"
                  Expanded="Expander_Expanded" Panel.ZIndex="99" Collapsed="Expander_Collapsed" Name="expander">

                <Popup PopupAnimation="Slide" Name="popup" Width="200" Height="200" StaysOpen="True" AllowsTransparency="True" 
                       IsOpen="False" >
                <Grid Background="Cornsilk">
                    <Grid.BitmapEffect>
                        <DropShadowBitmapEffect/>
                    </Grid.BitmapEffect>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                   <TextBlock TextWrapping="Wrap" FontWeight="Bold">
                      Some example text
                   </TextBlock>
                </Grid>
            </Popup>

        </Expander>
    </Canvas>
</Grid>

【问题讨论】:

    标签: wpf


    【解决方案1】:

    这几乎是 Rick 的答案,但在代码隐藏中,因为我认为 绝对没有理由引入另一个依赖项。

    protected override void OnLocationChanged(EventArgs e)
    {
        popup.HorizontalOffset += 1;
        popup.HorizontalOffset -= 1;
        base.OnLocationChanged(e);
    }
    

    只需复制并粘贴到您窗口的代码隐藏中,然后将 popup 标识符替换为您的弹出实例的标识符之一。


    我发现它好多了,更简洁,它不使用您必须取消订阅或内存泄漏的事件。
    对于您的应用程序的运行时,覆盖将具有更好的性能(没有什么值得注意的,但像您这样的微观性能非理性主义者仍然会喜欢它)。

    【讨论】:

      【解决方案2】:

      这是一个解决方案:

      这个仅标记示例由一个文本框和一个弹出窗口组成。当文本框获得焦点时,弹出窗口打开。弹出窗口在移动时跟随窗口移动,方法是挂钩窗口的位置更改事件并强制弹出窗口相应地重新定位。

      <Grid>
          <StackPanel>
              <TextBox x:Name="textBox1" Width="200" Height="20"/>
              <Popup PlacementTarget="{Binding ElementName=textBox1}" IsOpen="{Binding IsKeyboardFocused, ElementName=textBox1, Mode=OneWay}">
                  <TextBlock Background="White" Text="Sample Popup content."/>
                  <p:Attached.Operations>
                      <p:EventHandler Path="Loaded">
                          <p:ScriptHandler Path="@FindAncestor([Window], @AssociatedObject.PlacementTarget).LocationChanged">
                              @AssociatedObject.HorizontalOffset += 1;
                              @AssociatedObject.HorizontalOffset -= 1;
                          </p:ScriptHandler>
                      </p:EventHandler>
                  </p:Attached.Operations>
              </Popup>
          </StackPanel>
      </Grid>
      

      【讨论】:

      • 我试过这段代码和标记编程它工作得很好,但我唯一想知道的是如果窗口被拖到任务栏下方,弹出窗口会重新定位到屏幕中心并且永远不会出现背部。有没有办法处理这种情况?
      【解决方案3】:

      很遗憾,没有快速的解决方案。你可以使用我在这里描述的一个技巧:How to make WPF Combobox's Dropdown stays open & Placement

      【讨论】:

      • 谢谢。您会认为 MS 会在课程中提供此类功能。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 2017-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-08
      • 1970-01-01
      相关资源
      最近更新 更多