希望这对你来说是一个快速的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>
Run Code Online (Sandbox Code Playgroud)
Mas*_*tic 22
这几乎是Rick的答案,但在代码隐藏中,因为我认为没有理由引入另一个依赖.
protected override void OnLocationChanged(EventArgs e)
{
popup.HorizontalOffset += 1;
popup.HorizontalOffset -= 1;
base.OnLocationChanged(e);
}
Run Code Online (Sandbox Code Playgroud)
只需复制并粘贴到窗口的代码隐藏,然后用popup弹出的实例标识符替换标识符.
我发现它更好,它更简洁,它不会使用你必须取消订阅或内存泄漏的事件.
对于你的应用程序的运行时,覆盖将有更好的性能(没有什么值得注意的,但微观性能非理性主义者,如你真正的,仍然只是喜欢它).
这是使用的解决方案:
此仅标记示例包含一个文本框和一个弹出窗口。当文本框聚焦时,弹出窗口会打开。弹出窗口通过挂钩窗口的位置更改事件并强制弹出窗口相应地重新定位自身来跟随窗口移动。
<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>
Run Code Online (Sandbox Code Playgroud)