tags:

views:

1321

answers:

1

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. :)

  1. 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>
    
  2. 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!

+1  A: 

Here's what I would do, first I would separate my reusable parts into resources, and reference them using ContentControls in XAML. This applies to the Popup as well as to the Button. However I don't wish to restrict myself to having only a button, so I'll use a ContentControl as well for that:

The Popup template:

<ControlTemplate x:Key="PopupTemplate">
    <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>
</ControlTemplate>

The ContentControl template:

<ControlTemplate x:Key="MyControlTemplate" TargetType="ContentControl">
    <Grid Name="MyControl">
        <ContentPresenter Content="{TemplateBinding Content}"/>
        <Popup Name="MyPopup" StaysOpen="True" Placement="Bottom">
            <ContentControl Template="{StaticResource PopupTemplate}"/>
        </Popup>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger SourceName="MyControl" 
                 Property="UIElement.IsMouseOver" 
                 Value="True">
            <Setter TargetName="MyPopup" 
                    Property="Popup.IsOpen" 
                    Value="True"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

Now in the main window XAML, I would create the following:

<ContentControl Template="{StaticResource MyControlTemplate}">
    <Button Content="Test"/>
</ContentControl>

If you have any questions, I'll be happy to answer.

Aviad P.
Thanks for the help! It certainly sounds like a good direction to go in. I'll let you know how it goes!
Dave
It's almost working! My only issue now is that when I click somewhere else in the GUI, the Popup disappears (which is what I want), but when I hover over the ContentControl again, the Popup doesn't appear again. Do I have to explicitly set IsOpen to False in another trigger? Ideally, I would make the Popup disappear if I click somewhere that's *not* the Popup or ContentControl (i.e. some sort of compound/multi trigger).
Dave
I think the problem might be that you are setting `IsOpen` to false somewhere explicitly, thus overriding the trigger. Are you doing this in code-behind? Maybe in the handlers for the command bindings in the popup buttons? If so you need to then do clear its value to reenable the trigger... This might be tricky.
Aviad P.
I don't set IsOpen to False anywhere in code-behind, but I do have the StaysOpen property set to False, since I wanted it to disappear when I click elsewhere. I had forgotten about that. So should I set StaysOpen to True instead, and then add a MultiTrigger to set IsOpen to False?
Dave
Ok, I just went ahead and tried that, and the result was not what I had expected. If I set StaysOpen to True and then add the MultiTrigger, it behaves as expected, except that if I move from the ContentControl to the Popup too slowly, the Popup will disappear. The funny thing is, if I just set StaysOpen to True and *don't* implement the MultiTrigger, the behavior is exactly the same.
Dave
marked this as the answer, as it works very well. I am super excited, thanks for your help!
Dave
You're welcome!
Aviad P.