I created a Menu and inserted a MenuItem. For this MenuItem, I created 3 sub MenuItems.
When I select a sub MenuItem, it does not show up in the XAML editor.
<Window x:Class="CharakterTool3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CharakterTool3"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<Menu>
<MenuItem Header="File">
<MenuItem Header="Open"/>
<MenuItem Header="Close"/>
<MenuItem Header="Exit"/>
</MenuItem>
</Menu>
</StackPanel>
</Window>
This is what I expected:
In my Editor, it do not show up:
During the runtime, it is selectable:
I installed Microsoft Visual Studio Community 2019 Version 16.9.3
with Microsoft .NET Framework Version 4.8.04084.
What is the source of the problem?
This question already has answers here:
How do I open a WPF menu at design-time?
You can set IsSubmenuOpen="True", but if you click somewhere else, menu will close. Set it again to open.
Related
This is XAML codes.
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Menu x:Name="Menu1">
<MenuItem x:Name="MenuItem1" Height="40" Header="File">
<MenuItem x:Name="MenuItem11" Header="Help"/>
<MenuItem x:Name="MenuItem12" Header="About"/>
<MenuItem x:Name="MenuItem13" Header="Exit"/>
</MenuItem>
</Menu>
</Grid>
</Window>
How to expand sub menus from code behind?
Question picture: https://prnt.sc/img9p6
You will need a MenuViewModel class that has a ObservalbleCollection of SubMenuViewModel(s).
This class will need a property for the IsOpen for The Menu. https://msdn.microsoft.com/en-us/library/system.windows.controls.menuitem.issubmenuopen(v=vs.110).aspx
You should not create the MenuItems in WPF. But bind the Itemssource to thies collection.
You then bind the IsOpen from the MenuItem to the MenuItems in WPF (ItemsTemplate)
WPF - How can I create menu and submenus using binding
This morning I asked a question here and doing a simple working sample gave me a different behavior than expected.
Full working sample at GitHub. Main partial code below.
In this present case, the command is never propagated to any UserControl, either if the UserControl is use directly as a child of the Window. It also not work if the UserControl is used as a DataTemplate for a ListBox ItemTemplate.
I also include a hack button to fix the problem where the Command reach the UserControls. The hack come from StackOverflow.
But using the hack does not explain why UserControl does not receive the Command (without it) and using this hack also break the first rule of good coding: "Hi cohesion and Low coupling". The hack should be used in the the window code in order for it to manage the Command in the UserControl, my thought is that it should happen by default.
Why the command is not propagating by default to the UserControl and what should I do to propagate the command to the UserControl in a clean way?
Note: Using only one CommandBinding (removing one or the other) in the UserControl does not fix the problem .
Partial code:
<Window x:Class="CommandRoutingIntoItemTemplate.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CommandRoutingIntoItemTemplate"
xmlns:system="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<local:UserControlTest></local:UserControlTest>
<ListBox Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Aqua" BorderThickness="2">
<local:UserControlTest></local:UserControlTest>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Items>
<system:String>1</system:String>
<system:String>2</system:String>
</ListBox.Items>
</ListBox>
<StackPanel Grid.Row="2" Orientation="Horizontal">
<Button Command="local:Commands.CommandTest">Put focus on TestBlock and click here to see if command occurs</Button>
<Button Click="AddHack">Hack</Button>
</StackPanel>
</Grid>
</Window>
UserControl:
<UserControl x:Class="CommandRoutingIntoItemTemplate.UserControlTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CommandRoutingIntoItemTemplate"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.CommandBindings>
<CommandBinding Command="local:Commands.CommandTest" CanExecute="CommandTestCanExecuteUserControl" Executed="CommandTestExecuteUserControl"></CommandBinding>
</UserControl.CommandBindings>
<Grid>
<TextBox Text="UserControlTest">
<TextBox.CommandBindings>
<CommandBinding Command="local:Commands.CommandTest" CanExecute="CommandTestCanExecuteTextBox" Executed="CommandTestExecuteTextBox"></CommandBinding>
</TextBox.CommandBindings>
</TextBox>
</Grid>
</UserControl>
The reason that you are not getting commands invoked on your user control is that your buttons are not in separate focus scope. For WPF to pick up focused element for command target correctly, it needs to be in separate focus scope from command invoking control.
Framework will just traverse up visual tree from button looking for command bindings in their focus scope (in your case it won't find any). When framework does not find any command bindings in current focus scope, only then it looks into parent focus scope for focused element (In your case, buttons are in Window focus scope which has no parent scope so the search will end there).
Simply setting FocusManager.IsFocusScope="True" on your StackPanel will fix the issue.
You could also specify CommandTarget property on your buttons to point to your user control and not rely on focus.
This question already has an answer here:
CommandConverter cannot convert from System.String in WPF
(1 answer)
Closed 8 years ago.
<Window x:Class="CostelloM_Data_Persistence_v1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ContactMyPeeps(IllegalVersion)" Height="350" Width="525">
<Window.Resources>
<RoutedCommand x:Key="Saveas"/>
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="Saveas" Executed="Save_As"/>
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Key="S"
Modifiers="Control + Shift"
Command="Saveas"/>
</Window.InputBindings>
<Grid>
<StackPanel Orientation="Vertical">
<Menu>
<MenuItem Header="File">
<MenuItem Command="Save" Header="Save"/>
<MenuItem Command="Saveas" Header="Save as" InputGestureText="Ctrl+Shift+S"/>
<MenuItem Command="Open" Header="Open"/>
</MenuItem>
</Menu>
<ItemsControl>
<ComboBox>
</ComboBox>
</ItemsControl>
</StackPanel>
</Grid>
</Window>
My problem is as follows, the above code will not compile. I have tried saving, cleaning, and rebuilding my visual studio project and still no dice. It says that command converter cannot convert from system.string. Clearly I either misunderstand RoutedCommand and can not use in this way as a custom command. Is there either a way to force RoutedCommand to create a new command, or a different way to use a custom command?
You are defining a command in a resource so you need to tell the binding system that it is in a resource. You need to change several places in the XAML to this
Command="{StaticResource Saveas}"
However, there are several standard commands pre-defined for you in the ApplicationCommands class like Open, Save, and SaveAs. The binding system will automatically try to bind to these but casing is important. This:
Command="SaveAs"
will bind to the appropriate command defined in ApplicationCommands. Then the command defined in the resources becomes unnecessary.
You've got typos on several lines, which is most likely giving you a "CommandConverter cannot convert from System.String" error because it doesn't recognize the command.
Change this:
<CommandBinding Command="Saveas" Executed="Save_As"/>
To this: (uppercase "A")
<CommandBinding Command="SaveAs" Executed="Save_As"/>
I have a window:
<Window x:Class="SomeNamespace.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="350" Width="525">
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Copy"
CanExecute="CommandCanExecute" Executed="CommandExecuted"/>
</Window.CommandBindings>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="File">
<MenuItem Command="ApplicationCommands.Copy"/>
</MenuItem>
</Menu>
</DockPanel>
</Window>
With some code behind:
void CommandCanExecute(object sender, CanExecuteRoutedEventArgs e) {
e.CanExecute = true;
}
void CommandExecuted(object sender, EventArgs e) {
MessageBox.Show("Done!");
}
And everything works the way I expect. I can use the MenuItem or the Ctrl+C input binding to run my command.
But now my class has gotten too big, and I decide to refactor. So I moved my code behind to a user control. Here's my new Window:
<Window x:Class="SomeNamespace.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:SomeNamespace"
Height="350" Width="525">
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="File">
<MenuItem Command="ApplicationCommands.Copy"/>
</MenuItem>
</Menu>
<my:UserControl1/>
</DockPanel>
</Window>
And my UserControl:
<UserControl x:Class="ImageDecompileSandbox.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.CommandBindings>
<CommandBinding Command="ApplicationCommands.Copy"
CanExecute="CommandCanExecute" Executed="CommandExecuted"/>
</UserControl.CommandBindings>
</UserControl>
Basically, everything is the same, except the CommandBinding was moved from the window to UserControl and the two command methods were pushed down to the user control.
Question: Why does the above not work? Why is my User Control's command not picked up by the window? How do I get the MenuItem / KeyBindings from the window to work with the command execution in the User Control?
Thanks to dkozl, I was able to find a way to make this work.
The trick was indeed adding the CommandBinding back to the Window. Instead of declaring them all in the Window, which I can't do as the window doesn't know about the methods being used for Executed and CanExecute, I just added all the bindings from the control to the window:
CommandBindings.AddRange(_userControl1.CommandBindings);
I find this one-line hack to be exactly what I need, as it lets me keep the command controls and keybindings in the window while moving the command implementation to the control.
Thanks for the help dkozl!
I have a problem when creating a menu in WPF. What happens is that it closes automatically when you stop pressing the mouse button. I want it to behave as regular menu's where you can click and the subitems will stay up but I can't find anyway to get this done.
The code looks like this:
<Window x:Class="ExcelAddIn.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<DockPanel>
<Menu Width="Auto" IsMainMenu="True" >
<MenuItem Header="Item">
<MenuItem Header="SubItem" />
</MenuItem>
</Menu>
</DockPanel>
</Grid>
</Window>
I'm wondering if it has anything to do with logical focus maybe? I saw something about it might being a bug in .NET framwork? Any ideas?
Thanks in advance
I didn't think it made any difference at first but obviously it does. When running the code in a standalone WPF application it works, however when I try to open the WPF window from a Excel-addin project I get this problem..
Ok! I solved the problem. Turns out it was a focus problem after all.
When the excel addin executed the WPF window the excel window was still in focus. So on every mouseup the focus would jump back from WPF to excel.
All I had to do was change the execution from this:
MainWindow mainWindow = new MainWindow();
mainWindow.Activate();
mainWindow.Show();
to this:
MainWindow mainWindow = new MainWindow();
mainWindow.Activate();
mainWindow.ShowDialog();
Thanks for help anyway guys!
I adjusted your code a tiny bit (DockPanel.Dock="Top" and an extra grid to fill the rest of the dock panel). It works fine and the menu stays open. Does it work for you?:
<Window x:Class="WpfApplication2.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window3" Height="300" Width="300">
<Grid>
<DockPanel>
<Menu Width="Auto"
IsMainMenu="True" DockPanel.Dock="Top">
<MenuItem Header="Item">
<MenuItem Header="SubItem" />
</MenuItem>
</Menu>
<Grid />
</DockPanel>
</Grid>
</Window>