MVVM + WPF + Popup = clueless
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.