2

First of I should apologize if the question seems to look simple however I'm new to WPF and MVVM and I really don't know how to do the job.

What I need is to show popups from ViewModel. I know I can have a boolean property in the viewModel and bind it to the IsOpen property of the Popup but I don't know where to create this popup window.

I have some views and each view has to display certain popup messages depending on different situations. Now I need to know whether I should create several popups in each view and bind their IsOpen property to that of in the ViewModel or there are better solutions, and if I should create them in the view, where to put them? In a grid, in a StackPanel or anywhere else.

please let me know if I haven't explained clearly. Any help is appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3530012
  • 720
  • 2
  • 20
  • 37

2 Answers2

1

I usually have a Third object to control my popup's and dialog , like Caliburn's WindowManager

WindowManager

Witch takes a ViewModel as Content and Displays it's Corresponding View in the Popup.

You can do something similar and Bind a Content from your ViewModel to your popup or Dialog.

For Instance , here's an a Custom Action i created for such a purpose :

OpenPopupWindowAction

Community
  • 1
  • 1
eran otzap
  • 12,293
  • 20
  • 84
  • 139
0

this is sample popup. reference

basic popup msdn overview

<StackPanel>
<CheckBox Name="PCheckBox" Margin="10,10,0,0"
      Content="Popup Window"/>
<Button HorizontalAlignment="Left" Width="129" Margin="10,10,0,0">
    <Button.Triggers>
        <EventTrigger RoutedEvent="Button.Click">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation
        Storyboard.TargetName="theTransform"
        Storyboard.TargetProperty="(RotateTransform.Angle)"
        From="0" To="360" Duration="0:0:5" AutoReverse="True"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Button.Triggers>
    Start Animation
</Button>
<Popup  IsOpen="{Binding ElementName=PCheckBox,Path=IsChecked}"
   PlacementTarget="{Binding ElementName=PCheckBox}"           
   AllowsTransparency="True"
   PopupAnimation="Slide"
   HorizontalOffset="150"
   VerticalOffset="100"
   >

    <Canvas Width="100" Height="100" Background="Green" Margin="150">
        <Canvas.RenderTransform>
            <RotateTransform x:Name="theTransform" />
        </Canvas.RenderTransform>
        <TextBlock TextWrapping="Wrap" Foreground="LightGray">
  Rotating Popup
</TextBlock>
    </Canvas>
</Popup>

 private void OnPopupLoaded(object sender, RoutedEventArgs e)
    {
        this.ParentPopup.HorizontalOffset = (Window.Current.Bounds.Width - gdChild.ActualWidth) / 2;
        this.ParentPopup.VerticalOffset = (Window.Current.Bounds.Height - gdChild.ActualHeight) / 2;
    }
Eldho
  • 7,795
  • 5
  • 40
  • 77