Udineska
Udineska

Reputation: 27

Popup in WPF does not open

I have a datagrid with modified column header which contain a button which shall open a popup. This is written in code behind because of different data sources with different number of columns.

That's how it looks like:

enter image description here

Popups are stored in:

Dictionary<string, Popup> HeaderPopups = new Dictionary<string, Popup>();

And here the code behind:

dgMaterials.AutoGeneratingColumn += (ss, ee) =>
{
    Button b = new Button() { Content = "...", Name = "btn_" + ee.PropertyName, Margin = new Thickness(3) };
    b.Click += HeaderFilterButtonClick;
    StackPanel stackPanel = new StackPanel() { Orientation = Orientation.Horizontal };
    stackPanel.Children.Add(new TextBlock() { Text = ee.PropertyName, VerticalAlignment = VerticalAlignment.Center });
    stackPanel.Children.Add(b);
    ee.Column.Header = stackPanel;

    Popup pop = new Popup() { Name = "pop_" + ee.PropertyName, Placement = PlacementMode.Bottom, PlacementTarget = b, StaysOpen = false, Width = 200, Margin = new Thickness(3) };
    Border bord = new Border() { Background = Brushes.White, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(1,1,1,1) };
    pop.DataContext = bord;

    HeaderPopups.Add(ee.PropertyName, pop);

    StackPanel stack = new StackPanel() { Margin = new Thickness(5, 5, 5, 15) };
    bord.DataContext = stack;

    StackPanel stackButtons = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(0, 0, 0, 15) };

    Button bAll = new Button() { Margin = new Thickness(0, 0, 0, 0), Name = "btnAll_" + ee.PropertyName };
    bAll.Click += btnAllClick;
    TextBlock txtAll = new TextBlock() { Text = "Select All", Foreground = Brushes.Blue, Cursor = Cursors.Hand };
    bAll.Content = txtAll;

    Button bNone = new Button() { Margin = new Thickness(10, 0, 0, 0), Name = "btnNone_" + ee.PropertyName };
    bNone.Click += btnNoneClick;
    TextBlock txtNone = new TextBlock() { Text = "Select None", Foreground = Brushes.Blue, Cursor = Cursors.Hand };
    bNone.Content = txtNone;

    stackButtons.Children.Add(bAll);
    stackButtons.Children.Add(bNone);

    ListBox list = new ListBox() { Name = "lst_" + ee.PropertyName, BorderThickness = new Thickness(0) };

    stack.Children.Add(stackButtons);
    stack.Children.Add(list);
};

So for each column a popup is generated and I have the popups with the keys Spec_No, Grade and Class in my HeaderPopups dictionary.

I want the appropriate popups to show up beneath the clicked button, like in the example from http://www.jarloo.com/excel-like-autofilter-in-wpf/

Look here:

enter image description here

My problem is to open these popups in HeaderFilterButtonClick-Event. I tried it with:

private void HeaderFilterButtonClick(object sender, RoutedEventArgs e)
{
    Button b = sender as Button;
    txtTest.Text += e.OriginalSource.ToString() + Environment.NewLine;
    txtTest.Text += e.Source.ToString() + Environment.NewLine;
    txtTest.Text += b.Name;

    if (b.Name == "btn_Spec_No")
    {
        HeaderPopups["Spec_No"].IsOpen = true;
    }
}

but it doesn't work.

Can anybody help?

Upvotes: 1

Views: 718

Answers (2)

Whistler
Whistler

Reputation: 174

The popup is opening and rendering, but it is empty, so it can't be seen.

the problem is here

 Border bord = new Border() { Background = Brushes.White, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(1,1,1,1) };
 pop.DataContext = bord;

Datacontext is used to set Binding targets, which an empty popup has no bindings.

You need the fill the child object instead by changing the above into

 Border bord = new Border() { Background = Brushes.White, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(1,1,1,1) };
pop.Child = bord;

this sets the root of the popup container to ther Border object.
You will also have to do the same with the stack panel and border

StackPanel stack = new StackPanel() { Margin = new Thickness(5, 5, 5, 15) };
bord.Child = stack;

Upvotes: 1

mm8
mm8

Reputation: 169370

Your Popup is currently empty and thus completely invisible.

You should set the Child property of it to the Border and also set the Child property of the Border to something for it to render:

Popup pop = new Popup() { ... };
Border bord = new Border() { Background = Brushes.White, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(1, 1, 1, 1) };
bord.Child = new TextBlock() { Text = "some content..." };
pop.Child = bord;

Upvotes: 3

Related Questions