0

So, i have all of my controls nicely laid out using grids and stack panels.

In some cases, i would like to show a dialog layer (to add preferences etc). It does not need to be modal, and it does not need to have transparent background.. i just need it to be nice, unobtrusive, and positioned absolutely..

the only way i can think of to position such dialog control absolutely, is using a canvas, which i am not using. i need it to show on top of regularly laid out controls..

What are some options?

animuson
  • 53,861
  • 28
  • 137
  • 147
Sonic Soul
  • 23,855
  • 37
  • 130
  • 196
  • Why in the world not use a `Window`? – H.B. Jan 08 '12 at 18:32
  • You want to show the dialog entirely within another window, or outside of the borders of an existing window? – Chris Shain Jan 08 '12 at 18:37
  • as in popping up another window? that would be horrible. – Sonic Soul Jan 08 '12 at 18:37
  • i just want to hover it over existing controls, but within the same window.. something equivalent of floating div in html. – Sonic Soul Jan 08 '12 at 18:38
  • thanks, didn't know about adorner, reading up on it now – Sonic Soul Jan 08 '12 at 18:44
  • You can take a look at my answer on this one. It might be what you are looking for, I use it for all my MVVM based projects. http://stackoverflow.com/questions/8103743/wpf-c-sharp-inputbox/8103869#8103869 – eandersson Jan 08 '12 at 18:46
  • Fuji, thanks, but i also want to be able to absolute position my control.. it could span over multiple underlying controls.. – Sonic Soul Jan 08 '12 at 19:14
  • Do you need it to be absolutely positioned in the x,z,y cords, or rather that it's always on top? Simply setting it to a high Panel.ZIndex value would archive the last one. http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.zindex.aspx – eandersson Jan 08 '12 at 20:52
  • Fuji, i'd like to position it over other controls.. meaning.. i can't insert it into regular control flow, because i want it to span over multiple controls when it's visibile. kind of a modal dialog, accept it wouldn't be centered on the screen, rather within some x,y position.. could your method achieve that ? – Sonic Soul Jan 14 '12 at 19:14

3 Answers3

2

You can either use a popup control (set the PlacementRectangle value to position the content) or the Adorner layer to do this. The adorner technique is described here: http://bignickolson.com/2009/10/15/overlaying-controls-in-wpf-with-adorners/

Chris Shain
  • 50,833
  • 6
  • 93
  • 125
  • this is actually close to what i need. here i am facing a problem of non transparent background. my user control ha a border with rounded corners. if i show it in a popup, the corners are showing as white.. – Sonic Soul Jan 10 '12 at 01:24
  • 1
    Set AllowsTransparency = true on the popup control: http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.popup.allowstransparency.aspx – Chris Shain Jan 10 '12 at 01:24
  • thanks Chris. that was a step in right direction. any way to lock it down, so that it stays relative to the underlying window when that window is moved ? – Sonic Soul Jan 15 '12 at 19:47
  • There is a stackoverflow question for that already: http://stackoverflow.com/questions/1600218/how-to-move-a-wpf-popup – Chris Shain Jan 15 '12 at 20:57
0

I think what you are looking for is to set the Panel.ZIndex to a high value for your Dialog. Then simply set the visibility to hide the dialog. http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.zindex.aspx

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBlock Panel.ZIndex="9999"  Background="Green">This TextBlock will always be on top as long as it is visible.</TextBlock>
        <TextBlock Background="Red">Test</TextBlock>
    </Grid>
</Window>

You can then combine this with the Dialog box in this example: WPF C# InputBox

Community
  • 1
  • 1
eandersson
  • 25,781
  • 8
  • 89
  • 110
  • thanks Fuji, but does zIndex somehow allow for absolute positioning? in your example the control is still within that grid.. in my case it would be a user control that might be bigger than that grid.. thats why i need absolute positioning. – Sonic Soul Jan 10 '12 at 01:22
  • Yea, that is true. My solution only works if you want the dialog within that Grid, but as long as you keep it in the MainWindow Grid it should allow you to position it above everything else (within the Window Borders ofc). – eandersson Jan 28 '12 at 14:22
0

I believe this is what you are looking for.... You can Place your control in your adorner and then by a change of a bool property you can show or hide it... you can customize the behaviour to your likes

Ankesh
  • 4,847
  • 4
  • 38
  • 76