PopUp in Silverlight is not being clipped by container control - silverlight

Simple, yet frustrating issue here...
I have a PopUp control.
It is contained in side a Grid, which has a Grid.Clip defined.
The PopUp is still visible outside the Grid's clipped area.
i.e.
<Grid Background="Red" Width="150" Height="150">
<Grid.Clip>
<RectangleGeometry Rect="0,0,150,150" />
</Grid.Clip>
<Popup IsOpen="True" Margin="100,100,0,0">
<Grid Background="Green" Width="150" Height="150" />
</Popup>
</Grid>
Ideally, the green box should not appear or "bleed" outside of the red box. The problem is that it is contained within a PopUp, and so it bleeds. How can I modify this (without removing the PopUp control) so that the PopUp does not bleed outside of it's containing control?

Popup works differently. It "ignores" its parent and it is added directly into visual root of your app. This is how it can be on-top of everything.
So now it depends on what are you trying to do. I think popup is not suitable for this scenario.
You can try to clip the popup in its template, but I feel that's not what you want.

Related

How can I close a popup in WPF from inside its user control

I have implemented a toggle button with a popup similar to what is described here. My popup displays a user control that is basically a custom menu with several images/buttons on it. Here is the code for my toggle button:
<ToggleButton x:Name="btnMenu" Background="Transparent" HorizontalAlignment="Right">
<StackPanel>
<Image Width="60" Height="60" Margin="10,0,10,0" Source="{StaticResource icon-menu}"/>
<Popup StaysOpen="False" IsOpen="{Binding IsChecked, ElementName=btnMenu}" Placement="Bottom">
<controls:Menu/>
</Popup>
</StackPanel>
</ToggleButton>
Everything works fine, except that when I click on a button (which is bound to a command) in my user control, I would like the popup to disappear. Is there a way, through xaml, that I can close the parent popup when a button on the user control is pressed?
I did manage to find solutions on codeproject which would most likely solve the problem, and I also found various solutions on StackOverflow where all the xaml was inside the popup instead of a separate user control. I wanted to see if there was another preferred method (hopefully xaml based) that could solve this particular problem, and still let me keep my menu in a separate user control.

How to hide controls when it gets outside a canvas in WP7?

I'm working on a game using WP7 silverlight. Some controls are moving and at some point they get outside the canvas they where in.
I wonder why they are not hidden?
In windows forms when a control gets outside a panel for example, i.e:
control.left > panel.width
it disappears. Can this be possible in silverlight?
thanks..
You should use the Clip property.
The following will show a Button that will show outside of the Canvas because button width > canvas width:
<Canvas Width="200" Height="200">
<Button>My button with a lot of text</Button>
</Canvas>
Now if I add the Clip property, what goes outside of the clip region gets hidden:
<Canvas Width="200" Height="200">
<Canvas.Clip>
<RectangleGeometry Rect="0,0,200,200" />
</Canvas.Clip>
<Button>My button with a lot of text</Button>
</Canvas>

Popup Control VerticalAlignment doesn't "catch"

I'm using a Popup control, and for some reason its vertical alignment doesn't work as I expect. This is the XAML I have:
<Grid Width="100" Height="30" >
<TextBox Text="Hello" />
<Popup IsOpen="True" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" >
<Button Content="Hello" />
</Popup>
</Grid>
I expected that the Popup's Width would be 100px, the same as it's parent Grid.
However, the Button inside the popup behaves as if its HorizontalAlignment is Left, i.e., the Button's width is just enough to allow the word "Hello" inside of it, and I can't figure out why, or how to make it have the same width as the containing Grid.
Thanks!
Unfortunately, a Popup is not part of the same visual tree as the rest of the elements in the XAML in which it is defined. It is literally a new window, albeit a very simple and typically small window.
Put another way, the Popup doesn't really participate in the layout of the main window. So changing things like HorizontalAlignment has no effect because that property controls how layout positions and sizes this element relative to its containing element.
This "orphaned element" issue causes no end of problems when dealing with a Popup. Nevertheless, there are tools and techniques to address all the problems that being a separate window introduces.
The first suggestion I can give is to understand the placement properties, PlacementTarget and Placement. You can use these properties for positioning the Popup. You can also use the ElementName syntax of databinding to take care of sizing.
Here is an example of using these techniques in your situation:
<Grid Name="grid" Width="100" Height="30" >
<TextBox Text="Hello" />
<Popup IsOpen="True"
Placement="Bottom"
PlacementTarget="{Binding ElementName=grid}"
Width="{Binding ActualWidth, ElementName=grid}">
<Button Content="Hello" />
</Popup>
</Grid>

Silverlight Panels

I currently have two panels with controls on my page and when i set the top to hidden and bottom panel to visible the bottom panel is hovering down the middle of the page.
Is there a way to set this / use another control so that the 2nd panel will go to the top of the page.
thanks
<StackPanel>
<local:MyPanel1 Width="300" Height="100" Visibility="Collapsed" />
<local:MyPanel2 Width="300" Height="100" />
</StackPanel>
You can set the panels' visibility in codebehind, or better, databind it to your ViewModel (possibly using a ValueConverter to convert to the Visibility enum).

WPF: Visual studio like error buttons

I want to get this.
buttons http://www.shrani.si/f/X/6Y/24Jhn9D3/buttns.png
Everything works so far, buttons act as filter and are bind to the grid control.
All i want is the icons and counter on the button.
Whats the correct way of implementing those?
<ToggleButton x:Name="IsErrorShown" Margin="4" Width="100" Content="{lex:LocText Errors, Assembly=Client}">
I have tried adding image like this:
<ToggleButton x:Name="IsErrorShown" Margin="4" Width="100" Content="{lex:LocText Errors, Assembly=Client}">
<StackPanel>
<Image Source="Resources/Warning"/>
</StackPanel>
</ToggleButton>
but i get error that prop. Content is defined more then once.
A WPF Button (or ToggleButton) is a content control, into which you can put anything.
I haven't checked, but these buttons probably have a horizontal stack panel or a DockPanel, with an Image and then one or two TextBlocks. You could make a template for these, and also use binding to set the TextBlock Text content from your viewmodel.
Snoop ( http://snoopwpf.codeplex.com/ ) is a great tool for finding out how other people have built things in WPF.
The Adam Nathan WPF book is excellent, and if you don't have it you should get it. http://www.amazon.co.uk/Windows-Presentation-Foundation-Unleashed-WPF/dp/0672328917
Here's an example:
<ToggleButton Height="24" Width="100">
<DockPanel>
<Image Source="c:\\temp\\me.jpg" Margin="3"/>
<TextBlock Text="20 Errors"/>
</DockPanel>
</ToggleButton>

Resources