I'm still working on modifying my existing WPF application to be "compliant" with the MVVM approach. I'm getting pretty darned close, but am not sure how to solve one of remaining few hurdles.
I have a GUI element (currently a button, though it could be a label), where when the mouse enters it, it fires the MouseEnter event, and this creates a popup in the code-behind. The popup is composed of a simple stackpanel layout with several buttons. Each button's Click event currently is assigned to the same event handler, except that I change the Tag property to do a simple (cheesy) command parameter. I'm wrestling with the "correct" way to do this in MVVM, because the way I'm doing it now is pretty ugly.
In order to solve this, I think this is the direction I should head in, but would appreciate any additional input you can offer me. :)
Create Popup in XAML. Since my Popup contents are static, I should be able to create the Popup entirely in XAML. In addition, each button would be bound to the same ICommand-derived class. e.g.
<Popup x:Key="MyPopup" StaysOpen="False" Placement="Right"> <Border Background="White" BorderBrush="Black" Padding="5" BorderThickness="2" CornerRadius="5"> <StackPanel Orientation="Vertical"> <Button Command="{Binding MyCommand}" CommandParameter="5">5</Button> <Button Command="{Binding MyCommand}" CommandParameter="10">10</Button> <Button Command="{Binding MyCommand}" CommandParameter="15">15</Button> <Button Command="{Binding MyCommand}" CommandParameter="20">20</Button> </StackPanel> </Border> </Popup>
Make the Popup pop up via a Trigger, e.g.
<Button.Triggers> <Trigger Property="Button.IsMouseOver" Value="True"> <Setter TargetName="MyPopup" Property="IsOpen" Value="True" /> </Trigger> </Button.Triggers>
I think #1 is ok, but I am struggling with #2. Is this the right way to approach the problem? And if so, what's the correct XAML syntax for getting the Popup's IsOpen property to be set to True? I couldn't find examples for this.
If my whole thinking is flawed, I would love to hear what my other options are. Thanks!