Patrick K.

Hi everyone,

I am thinking about creating a tool-bar-like element for my application. This tool bar should be a non-modal window which hosts several buttons. Each button should 'trigger' a specified command. The tricky part is that the commands should 'trigger' on the main window of my application (the tool bar doesn't have any implementation). - So basically I want to rip out the menu of my normal window and put it in an extra window while keeping the behavior...

I thought about it a while and I don't see that this is possible but maybe someone here has some good remarks about it :-)

any hints are very much appreciated

Patrick


Re: Windows Presentation Foundation (WPF) executing Commands on a different window

Patrick Klug

-- changed the title --




Re: Windows Presentation Foundation (WPF) executing Commands on a different window

Eddie Li - MSFT

You can get a reference of the main window by accessing Application.MainWindow property or through Application.Windows collection if that's not the main window.




Re: Windows Presentation Foundation (WPF) executing Commands on a different window

Patrick Klug

sorry, I don't understand how that should help me...




Re: Windows Presentation Foundation (WPF) executing Commands on a different window

Eddie Li - MSFT

Perhaps I misinterpreted your scenario. Can you describe what commands are you trying to issue to the mainwindow from your toolbar window




Re: Windows Presentation Foundation (WPF) executing Commands on a different window

Patrick Klug

sure. All sorts of commands. Example:

I've a textbox on the mainwindow and the paste command in a button on the toolbar window. Now, when the command updates its state the CanExecute will always return false because the toolbar window doesn't have a textbox thus no one implements the command. Ultimately I want the Commands to query and execute on the mainwindow and not on the window where the button is. Since I have a lot of Commands on the toolbar window (and they could even be loaded dynamically) I can't set a CommandTarget...

Its a bit like a popup menu. When I understand it correctly a popup menu is technically another window but the commands 'triggered' in this window are executed on the owning window. I wonder how a popup achieves that behavior.









Re: Windows Presentation Foundation (WPF) executing Commands on a different window

Eddie Li - MSFT

I hope I understood it correctly this time. What you can do is to set CommandTarget property on the button to point to an object on your main window. For example, the following code enables you to execute Paste command on a window contains a TextBox control:

Code Snippet

Window TextBoxWindow = new Window();
TextBoxWindow.Content = new TextBox();
TextBoxWindow.Show();

Button PasteButton = new Button();
PasteButton.Content = "Paste";
PasteButton.Command = ApplicationCommands.Paste;
PasteButton.CommandTarget = TextBoxWindow.Content as TextBox;

Window ToolBarWindow = new Window();
ToolBarWindow.Content = PasteButton;
ToolBarWindow.Show();






Re: Windows Presentation Foundation (WPF) executing Commands on a different window

Patrick Klug

Hi Eddie,

Thanks for your answer. very much appreciated.

Unfortunately I don't know what elements are in my window and which implement commands therefore I can't set the CommandTarget....

I came up with a nasty hack, which I don't plan to use but it shows what end result I want to achieve.

Code Snippet

Window w = new Window();

w.Content = pasteButton;

foreach (Visual v in EnumVisual(this))
{
UIElement element = v as UIElement;

if (element != null)
{
w.CommandBindings.AddRange(element.CommandBindings);
}
}

w.Show();
}

private IEnumerable<Visual> EnumVisual(Visual myVisual)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
{
Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);

yield return childVisual;

foreach (Visual v in EnumVisual(childVisual))
{
yield return v;
}
}
}


In effect this code copies the commandbindings of the main window (and all its children) to my new window. It produces the effect that I want but of course I would prefer just rerouting the commands to the main window.