可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a Popup that looks like this:
<local:toolbox_popup x:Name="PopupBaseMapLayers" Grid.Row="1" StaysOpen="False" PopupAnimation="Fade" VerticalAlignment="Stretch" AllowsTransparency="True" PlacementTarget="{Binding ElementName=tooldropdown}" PlacementRectangle="-280,0,0,0" > <StackPanel> <ScrollViewer VerticalScrollBarVisibility="Auto" VerticalAlignment="Stretch" > </SrollViewer> </StackPanel> </local:toolbox_popup>
So the Popup appears when the user clicks a button, but the popup overflows outside the window because of how long it is. Is there a way to either have the overflow hidden within the window or set the height of the popup to be dynamic with whatever the size of the window is?
回答1:
A lot many things needs to be considered in your question. What should be the minimum width which would be feasible, where exactly are the buttons which are used to show this Popup. Are these Buttons are at right bottom of the Window ?
Consider everything, I suggest following settings which will solve your problem.
<Popup x:Name="Popup1" Placement="Relative" PlacementTarget="{Binding ElementName=Grd}" StaysOpen="False" Width="{Binding PlacementTarget.ActualWidth, RelativeSource={RelativeSource Self}}"> ... </Popup>
Here I have set PlacementTarget to be the top most container Grid. StatysOpen = False causes Popup to disappear if we click anywhere outside. You can even choose Window as your PlacementTarget.
Then there is another property called PlacementRectangle to place it, you can apply Binding with a Converter to return Rectangle.
Let me know if this solves your problem.
回答2:
As per MSDN reference on Popup Size a popup resizes according to content.
By default, a Popup is automatically sized to its content. When auto-sizing occurs, some bitmap effects may be hidden because the default size of the screen area that is defined for the Popup content does not provide enough space for the bitmap effects to display.
Popup content can also be obscured when you set a RenderTransform on the content. In this scenario, some content might be hidden if the content of the transformed Popup extends beyond the area of the original Popup. If a bitmap effect or transform requires more space, you can define a margin around the Popup content in order to provide more area for the control. Perhaps your Popup content is having sizing issues due to you restricting growth within your layout container? Your Popup is thus sized accordingly but your Grid for example may have a fixed width.
Anyways you can try setting the width and height of popup according to its parent window in the popup constructor like this :
this.Height = Application.Current.MainWindow.Height; this.Width = Application.Current.MainWindow.Width;