How can I open a modal popup on a user control (not on a mainwindow) in wpf using mvvm pattern?
I hope my question is clear to all as I want to open popup on usercontrol not on window.
How can I open a modal popup on a user control (not on a mainwindow) in wpf using mvvm pattern?
I hope my question is clear to all as I want to open popup on usercontrol not on window.
Use an Adorner.
public class OpaqueAdorner : Adorner
{
public OpaqueAdorner(UIElement win)
: base(win)
{ }
protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
{
SolidColorBrush renderBrush = new SolidColorBrush(Colors.LightCoral);
renderBrush.Opacity = 0.3;
Pen renderPen = new Pen(new SolidColorBrush(Colors.DarkBlue), 5.0);
drawingContext.DrawRectangle(renderBrush, renderPen,
new Rect(new Point(0, 0), AdornedElement.DesiredSize));
}
}
/* Grd is the container Grid */
private void Btn_Click(object sender, RoutedEventArgs e)
{
//GetAdornerLayer was missing ')'
AdornerLayer.GetAdornerLayer(Grd).Add(new OpaqueAdorner(Grd));
}
Combine this concept with Opened / Closed
event of Popup
.
I know it's an old question, but when I did this search, I find a lot of related question, but I did not find a really clear response. So I make my own implementation of a dialogbox/messagebox/popin, and I share it !
https://stackoverflow.com/a/40135791/2546739
It is showing things like this :
Modal is meant for new windows not usercontrols. A usercontrol is just a control inside a Page or Window.
Hence if you want to make it modal, then make the user control cover then entire area of your window and it can only be closed once they have completed what was in it. Maybe even make some of it partially transparent so it looks like a popup if you wish.
I don't know this is the one which you are expecting . Opening a model pop up from wpf is as same as from opening one in normal windows app
**ModalWin objWin = new ModalWin();
objWin.ShowDialog();**
Regards Sree
Modal generally means modal to an application, or in the case of a popup, for a window. Not for a specific control.
If you want to disable a specific control while displaying a popup, you could do so in a trigger:
<Popup x:Name="popup">
<!-- ... -->
</Popup>
<UserControl>
<UserControl.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding IsOpen, ElementName=popup}"
Value="True">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Style>
<UserControl>
This is actually a fairly involved topic.
Stackoverflow has a lot of info on dialog boxes in MVVM because using a solution such as Sree's breaks MVVM
here is a good spot to start Handling Dialogs in WPF with MVVM
Also, opening a popup on the usercontrol or the mainwindow will be the same.