user2155957
user2155957

Reputation: 21

popup control with a button bound to combobox

I have a popup control that has a button in it. The popup should be shown when a combobox is opened and should disappear when it closes. My code looks like this:

<Popup Name="myPopup" IsOpen="{Binding Path=IsDropDownOpen, ElementName=combo}" 
       Placement="Right" PlacementTarget="{Binding ElementName=combo}" 
       StaysOpen="False" Focusable="False" 
               AllowsTransparency="True" PopupAnimation="Fade" >
     <Button Click="Popup_Click">
        <Image Source="..\Images\edit.png" Height="30"/>
     </Button>
 </Popup>

 <ComboBox Name="combo"  SelectedValuePath="Key" DisplayMemberPath="Value" 
           Loaded="ComboBox_Loaded" SelectedValue="{Binding FamilyStatus}">
 </ComboBox>

And the functions:

 private void ComboBox_Loaded(object sender, RoutedEventArgs e)
 {
    combo.ItemsSource = GetComboValues();   
 }

In this code I have a problem: The popup is shown when the combobox is opened and get closed when the combo is closed but the click button (from inside the popup) isn't fired! I guess it is because that while the mouse moves to the button the "IsOpen" of the popup sets to be false so the click isn't fired anymore. I tries a different approach setting the IsOpen="False" and opening the popup with the combobox event:

DropDownOpened="ComboBox_Loaded_DropDownOpened" and the StaysOpen="False"

private void ComboBox_Loaded_DropDownOpened(object sender, EventArgs e)
{
   myPopup.IsOpen = true;
   myPopup.StaysOpen = false;
}

But then the popup stays open even if I select a different textbox on the window and the click event of the button is raised only after two clicks. (If I add a DropDownClosed="ComboBox_Loaded_DropDownClosed" event to close the popup, again the popup disappears before I can press the button). Could anybody help with a solution?

Upvotes: 1

Views: 1639

Answers (1)

user1064519
user1064519

Reputation: 2190

You should retemplate your combobox, so the property IsDropDownOpen will not affect it anymore and then you can force the combo to stay open even if he lost focus. I used the property Tag to replace IsDropDownOpen and also set StaysOpen = true on both popups (also in comboBox template)

here is the code :

ComboBox Template:

<Style x:Key="ComboBoxStyle1" TargetType="{x:Type ComboBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBox}">
                    <Grid x:Name="MainGrid" SnapsToDevicePixels="true">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
                        </Grid.ColumnDefinitions>
                        <Popup x:Name="PART_Popup" StaysOpen="True" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
                            <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=MainGrid}">
                                <Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
                                    <ScrollViewer x:Name="DropDownScrollViewer">
                                        <Grid RenderOptions.ClearTypeHint="Enabled">
                                            <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                                <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName=DropDownBorder}"/>
                                            </Canvas>
                                            <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                        </Grid>
                                    </ScrollViewer>
                                </Border>
                            </Microsoft_Windows_Themes:SystemDropShadowChrome>
                        </Popup>
                        <ToggleButton BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" IsChecked="{Binding Tag, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}">
                            <ToggleButton.Style>
                                <Style TargetType="{x:Type ToggleButton}">
                                    <Setter Property="OverridesDefaultStyle" Value="true"/>
                                    <Setter Property="IsTabStop" Value="false"/>
                                    <Setter Property="Focusable" Value="false"/>
                                    <Setter Property="ClickMode" Value="Press"/>
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type ToggleButton}">
                                                <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" SnapsToDevicePixels="true">
                                                    <Grid HorizontalAlignment="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
                                                        <Path x:Name="Arrow" Data="M 0 0 L 3.5 4 L 7 0 Z" Fill="Black" HorizontalAlignment="Center" Margin="3,1,0,0" VerticalAlignment="Center"/>
                                                    </Grid>
                                                </Microsoft_Windows_Themes:ButtonChrome>
                                                <ControlTemplate.Triggers>
                                                    <Trigger Property="IsChecked" Value="true">
                                                        <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
                                                    </Trigger>
                                                    <Trigger Property="IsEnabled" Value="false">
                                                        <Setter Property="Fill" TargetName="Arrow" Value="#AFAFAF"/>
                                                    </Trigger>
                                                </ControlTemplate.Triggers>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </ToggleButton.Style>
                        </ToggleButton>
                        <ContentPresenter ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="false" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true">
                            <Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/>
                            <Setter Property="Color" TargetName="Shdw" Value="#71000000"/>
                        </Trigger>
                        <Trigger Property="HasItems" Value="false">
                            <Setter Property="Height" TargetName="DropDownBorder" Value="95"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            <Setter Property="Background" Value="#FFF4F4F4"/>
                        </Trigger>
                        <Trigger Property="IsGrouping" Value="true">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                        </Trigger>
                        <Trigger Property="ScrollViewer.CanContentScroll" SourceName="DropDownScrollViewer" Value="false">
                            <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}"/>
                            <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Your code :

   <Popup x:Name="myPopup" IsOpen="{Binding Tag, ElementName=combo}" 
   Placement="Right" PlacementTarget="{Binding ElementName=combo}" 
   StaysOpen="True" Focusable="False" 
           AllowsTransparency="True" PopupAnimation="Fade" >
            <Button Click="Popup_Click">
                <Image Source="..\Images\edit.png" Height="30"/>
            </Button>
        </Popup>

        <ComboBox Name="combo"  SelectedValuePath="Key" DisplayMemberPath="Value" 
       Loaded="ComboBox_Loaded" SelectedValue="{Binding FamilyStatus}" Style="{StaticResource ComboBoxStyle1}">
        </ComboBox>

Upvotes: 0

Related Questions