Right aligned popup menu with GTK#
I have a button on the far right of an application. When clicked I want to show a menu underneath the button but right aligned with it. (think google chrome menu).
This is what I have so far:
protected virtual void HandleMenuClicked (object sender, System.EventArgs e)
{
Menu menu = new Menu();
menu.Add(new MenuItem("About"));
menu.ShowAll();
menu.Popup(null,null,MenuPosition,0,0);
}
private void MenuPosition(Menu menu, out int x, out int y, out bool pushIn)
{
_menuButton.ParentWindow.GetOrigin(out x, out y);
int menuWidth =开发者_JAVA技巧 75; //need to look up actual width.
x += _menuButton.Allocation.X - menuWidth;
y += _menuButton.Allocation.Y + _menuButton.Allocation.Height;
pushIn = true;
}
The problem with this is that I have hard coded the width of the menu. If a longer menu entry was added, the font changed or theme changed then the alignment will be wrong.
I tried looking up the width of the menu as follows:
int menuWidth = menu.Allocation.Width;
This does not work as the menu has not been drawn yet and so the allocation width is 1.
I believe you can use SizeRequest at that point.
精彩评论