I have this Menu in my App:
<Menu Height="23" HorizontalAlignment="Left" Margin="84,66,0,0" Name="menu1" VerticalAlignment="Top" Width="200">
<MenuItem Header="File" />
<MenuItem Header="Youtube" Name="myYouTube" Click="MenuItem_Click">
<MenuItem Header="Login"/>
<MenuItem Header="Logout"/>
</MenuItem>
<MenuItem Header="Help" />
</Menu>
And when i click myYouTube then MenuItem_Click won't fire.
Any idea why i happen?
If you have sub-menu items the event will not be raised se: MenuItem.Click Event
But maybe you could use the SubmenuOpened event se: MenuItem.SubmenuOpened Event
I changed it to the events for WPF now.
Related
Instead of using Alt + F to open the menu "File", I want to change the holy key F1, how do I do that? Thanks!
<Grid>
<Menu Background="Transparent">
<MenuItem x:Name="asd" Header="_File">
<MenuItem x:Name="a" Header="1. _New" InputGestureText="ALT-N" Margin="0,18,0,0"/>
<MenuItem Header="2. _Open" InputGestureText="Alt+O"/>
<MenuItem Header="3. _Close"/>
<MenuItem Header="4. Save"/>
<Separator/>
<MenuItem Header="5. Delete" Margin="0,8,0,0"/>
<MenuItem Header="6. Rename"/>
<Separator/>
<MenuItem Header="7. Print" Margin="0,8,0,8"/>
<Separator/>
<MenuItem Header="8. Format Disk" Margin="0,8,0,8"/>
<Separator/>
<MenuItem Header="9. MINE(Decode)" Margin="0,8,0,8"/>
</MenuItem>
</Menu>
</Grid>
One way of getting the behaviour you desire would be to add an PreviewKeyDown event listener to your Grid or the parent.
<Grid PreviewKeyDown="Grid_PreviewKeyDown">
<Menu Background="Transparent">
<MenuItem x:Name="asd" Header="_File">
<MenuItem x:Name="a" Header="1. _New" InputGestureText="ALT-N" Margin="0,18,0,0"/>
<MenuItem Header="2. _Open" InputGestureText="Alt+O"/>
<MenuItem Header="3. _Close"/>
<MenuItem Header="4. Save"/>
<Separator/>
<MenuItem Header="5. Delete" Margin="0,8,0,0"/>
<MenuItem Header="6. Rename"/>
<Separator/>
<MenuItem Header="7. Print" Margin="0,8,0,8"/>
<Separator/>
<MenuItem Header="8. Format Disk" Margin="0,8,0,8"/>
<Separator/>
<MenuItem Header="9. MINE(Decode)" Margin="0,8,0,8"/>
</MenuItem>
</Menu>
</Grid>
Then in the underlying C# code add the following action event
private void Grid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.F1) // This filters what key was pressed
{
asd.IsSubmenuOpen = true; // This opens the menu
e.Handled = true; // Setting this to true prevents the any other events from occurring due to the key press
}
}
I've created a controll menu in WPF. The button in that menu should activate the "Dark mode" that I've already created. Well that's just a simple Click event.
But I'd like to have a CheckBox not a simple Click event.
That's my code in XAML:
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_Settings">
<MenuItem Header="_Darkmode" />
</MenuItem>
</Menu>
</DockPanel>
The C# Code is inconsequential because that's just a sample.
Greetings from the US
Instead of using the Click event:
<Menu>
<MenuItem Header="_Settings">
<MenuItem Header="_Darkmode"
Click="OnDarkmodeClicked"/>
</MenuItem>
</Menu>
You could use the Checked/Unchecked events after setting the IsCheckable property:
<Menu>
<MenuItem Header="_Settings">
<MenuItem Header="_Darkmode"
IsCheckable="True"
Checked="OnDarkmodeChecked"
Unchecked="OnDarkmodeUnchecked"/>
</MenuItem>
</Menu>
Or you could bind IsChecked to a property in your model:
<Menu>
<MenuItem Header="_Settings">
<MenuItem Header="_Darkmode"
IsCheckable="True"
IsChecked="{Binding UseDarkmode}"/>
</MenuItem>
</Menu>
This may not be the best solution, but you could store a boolean elsewhere, and have a menuitem that looks like this:
XAML code
<MenuItem Header="_Blur Background" IsCheckable="True" IsChecked="True" Click="BlurOnOff_Click" InputGestureText="Ctrl+B"></MenuItem>
Then C# code
private void BlurOnOff_Click(object sender, RoutedEventArgs e)
{
blurOn = blurOn ? false : true;
}
The Blur On/Off is just an example. When the function is called, it updates the boolean to be the opposite of what it was before.
I built a menu bar using the menu control in WPF and it had been working but at some point the menu started showing up on the top left of my first monitor regardless of where in the screen I had the window. Even if I move the main window to the second monitor the menu still shows up in the first monitor.
Here is the code for the menu control:
<Menu>
<MenuItem Header="_File">
<MenuItem Header="_New" Command="New" InputGestureText="Ctrl+N"/>
<MenuItem Header="_Open" Command="Open" InputGestureText="Ctrl+O" />
<MenuItem Header="_Close" Command="Close" InputGestureText="Ctrl+W" />
<Separator/>
<MenuItem Header="_Save" Command="Save" InputGestureText="Ctrl+S" />
<MenuItem Header="Save _As" Command="SaveAs" InputGestureText="Ctrl+Shift+S" />
<Separator/>
<MenuItem Header="E_xit" Command="{StaticResource CommandBinding_Exit}" InputGestureText="Ctrl+Q" />
</MenuItem>
<MenuItem Header="_Edit">
<MenuItem Header="_Add" Command="{StaticResource CommandBinding_Add}" InputGestureText="" />
<MenuItem Header="_Edit" Command="{StaticResource CommandBinding_Edit}" InputGestureText="" />
<MenuItem Header="_Delete" Command="Delete" InputGestureText="" />
<Separator/>
<MenuItem Header="Cut" Command="Cut" InputGestureText="Ctrl+X" />
<MenuItem Header="Copy" Command="Copy" InputGestureText="Ctrl+C" />
<MenuItem Header="Paste" Command="Paste" InputGestureText="Ctrl+V" />
</MenuItem>
<MenuItem Header="_View">
<MenuItem x:Name="miShowStatusBar" Header="Show Status Bar" IsCheckable="True" IsChecked="True" Click="miShowStatusBar_Click"/>
<MenuItem x:Name="miShowFullPath" Header="Show Full Path" IsCheckable="True" IsChecked="True" Click="miShowFullPath_Click"/>
</MenuItem>
<MenuItem Header="_Help">
<MenuItem Header="_About"/>
</MenuItem>
</Menu>
The menu is not referenced any where in the code behind so I can't figure out what might be causing this odd menu placement.
I think the source of the OP's problem is in the search box under the MenuBar. It may come from the Margin or Width settings of the search box.
I faced the same problem and found the sources. Here is the boilerplate code to reproduce the same error. The problem is a combination of 5 parts, as shown in the following XAML. Remove any one of them will solve the OP's issue.
You need to bind the XAML View to a ViewModel with public property Films. If the Films collection has any element in it, it will also cause the issue (Part 5).
<Grid x:Name="MainContent">
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition />
</Grid.RowDefinitions>
<Grid >
<Menu>
<MenuItem Header="Film">
<MenuItem Header="New"/>
</MenuItem>
</Menu>
</Grid>
<Grid Grid.Row="1">
<Grid>
<!--Part 1: Remove HorizontalScrollBarVisibility="Auto"-->
<ScrollViewer HorizontalScrollBarVisibility="Auto">
<!--Part 2: Remove ItemsSource="{Binding Films}"-->
<ItemsControl ItemsSource="{Binding Films}">
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<!--Part 3: Remove the setter tag-->
<Setter Property="Margin" Value="2" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button>
<Button.Template>
<ControlTemplate TargetType="Button">
<!--Part 4: Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, Path=ActualWidth}-->
<ContentPresenter Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, Path=ActualWidth}"/>
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Grid>
</Grid>
</Grid>
This has nothing to do with WPF. It's a possible corruption of the Handedness setting or driver issues if you have a touch screen monitor, tablet PC or have a tablet attached with a bad driver (such as a Wacom drawing tablet).
Type this into the Run dialog: shell:::{80F3F1D5-FECA-45F3-BC32-752C152E456E}.
Once Tablet PC Settings come up, go to the Other tab and in the Handedness section, check the Left Handed option.
I bet that this has something to do with the Xaml-Designer of your Visual Studio instance. I encounter similar strange things because of leaving the Xaml-Designer open while debugging.
Try to kill the XDesProc.exe process and check if the problem still occurs.
Inside this combobox I have a menu that contains multiple menus.
This is how it looks:
This is the XAML:
<ComboBox>
<Menu>
<MenuItem Header="Name">
<MenuItem Header="Last" />
<MenuItem Header="First" />
</MenuItem>
</Menu>
<Menu>
<MenuItem Header="Age">
<MenuItem Header="20" />
<MenuItem Header="24" />
</MenuItem>
</Menu>
</ComboBox>
When the user selects "Last"/"First"/"20"/"24" I want the combobox to set its selected item to one of this 4 items.
Is it possible? If yes, how can I do it?
Yes you can do that. But I am not sure if this is the best way cos I dont know your requirement in full.
XAML:
<ComboBox x:Name="cmb1" IsEditable="True" >
<Menu>
<MenuItem Header="Name" >
<MenuItem Click="MenuItem_Click" CommandParameter ="Last" Header="Last"/>
<MenuItem Click="MenuItem_Click" CommandParameter="First" Header="First" />
</MenuItem>
</Menu>
<Menu>
<MenuItem Header="Age">
<MenuItem Header="20" />
<MenuItem Header="24" />
</MenuItem>
</Menu>
</ComboBox>
vb Code:
Private Sub MenuItem_Click(sender As Object, e As RoutedEventArgs)
Me.cmb1.Text = CType(sender, MenuItem).CommandParameter.ToString()
End Sub
c# code :
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
MenuItem menuItem = sender as MenuItem;
this.cmb1.Text = menuItem.CommandParameter.ToString();
}
According to this code:
<StackPanel>
<StackPanel.ContextMenu>
<ContextMenu>
<MenuItem Header="menuitem1"/>
<MenuItem Header="menuitem2"/>
</ContextMenu>
</StackPanel.ContextMenu>
<Button Width="100" Height="100"/>
<Button Width="100" Height="100"/>
</StackPanel>
If you right click on the Buttons then ContextMenu will appear, children will inherit their parent’s ContextMenu.
My question is how can I prevent this feature?
Edit: I need a way in xaml if it's possible.
I found this solution
<StackPanel>
<StackPanel.ContextMenu>
<ContextMenu>
<MenuItem Header="menuitem1"/>
<MenuItem Header="menuitem2"/>
</ContextMenu>
</StackPanel.ContextMenu>
<Button Width="100" Height="100">
<Button.ContextMenu>
<ContextMenu Visibility="Hidden"/>
</Button.ContextMenu>
</Button>
<Button Width="100" Height="100"/>
</StackPanel>
On the buttons in question, you need to stop a right-click mouse event propagating up to the containing StackPanel. You can do this by handling MouseDown like this:
void button_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.RightButton == MouseButtonState.Pressed)
{
e.Handled = true;
}
}