mahboub_mo
mahboub_mo

Reputation: 3038

What kind of panel should I use to create a custom dropdown?

I need to create a custom dropdown that has a button to open it, and a panel to show something, and i want to get it to close when click on outside of it, what kind of panel should i use? I tried popupmenu but I couldn't make it close after lostfocus, and tried contextmenu but it get close on click on it's inner item.

Any suggestion whould be appreciated.

I tried something like this:

    <Button x:Name="toggleButton"  
            ContextMenuService.Placement="Top"  Click="ToggleButton_Click"
            Margin="0" Content="+">
        <Button.ContextMenu>
            <ContextMenu Width="200" 
                         HorizontalContentAlignment="Stretch"
                         VerticalContentAlignment="Stretch" >
                <StackPanel>
                    <DataGrid >
                        <DataGrid.Columns>
                            <DataGridTextColumn/>
                            <DataGridTextColumn/>
                        </DataGrid.Columns>
                    </DataGrid>
                    <TextBlock Text="sadfasdfas" />
                    <TextBox Text="" Width="100"/>
                </StackPanel>
            </ContextMenu>
        </Button.ContextMenu>
    </Button>

Upvotes: 1

Views: 1579

Answers (2)

mahboub_mo
mahboub_mo

Reputation: 3038

finally i created what i needed after 2 days!!,and i really coulden't find a better solution,i had to put two button,on for showing the popup and another for closing the popup!

in xaml

    <ToggleButton x:Name="ShowPopup" Width="20" Height="20" Click="buttonShowPopup_Click"  Panel.ZIndex="1" />
    <ToggleButton x:Name="ClosePopup" Width="20" Height="20" Click="ClosePopupPopup_Click"  />
    <Popup x:Name="popup" 
        HorizontalOffset="0"
        VerticalOffset="0"
        PlacementTarget="{Binding ElementName=buttonShowPopup}"
        Placement="Top"
        PopupAnimation="Slide"
        AllowsTransparency="True"
        Focusable="True"

        StaysOpen="False" Margin="36,0,-36,0">
        <Grid LostFocus="Grid_LostFocus" >
            <TextBox Text="asdasd"/>
        </Grid>
    </Popup>

in codebehind

 public DropDown()
    {
        InitializeComponent();
        popup.Closed += popup_Closed;
    }

    private void ClosePopupPopup_Click(object sender, RoutedEventArgs e)
    {
        popup.IsOpen = false;
    }
    private void buttonShowPopup_Click(object sender, RoutedEventArgs e)
    {
        popup.IsOpen = true;
        ClosePopup.SetValue(Canvas.ZIndexProperty, 1);
        ShowPopup.SetValue(Canvas.ZIndexProperty, 0);
    }

    void popup_Closed(object sender, EventArgs e)
    {
        ShowPopup.SetValue(Canvas.ZIndexProperty, 1);
        ClosePopup.SetValue(Canvas.ZIndexProperty, 0);
    }

Upvotes: 1

Muad&#39;Dib
Muad&#39;Dib

Reputation: 29196

you should use a Popup control. here is the MSDN documentation and here is an article to help point the way

Upvotes: 2

Related Questions