Bind WPF combobox submenu item to combobox selecteditem - c#

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();
}

Related

press F1 to open sub menuitem in wpf C#

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
}
}

WPF | Control Menu Checkbox (C#)

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.

WPF MenuItem multi adding.

Error CS0120 An object reference is required for the non-static field,
method, or property 'MainWindow.System' %Path%\MainWindow.g.cs at line
316
I get the code error i mentioned on title when i try to add Sub MenuItems under a SubMenuItem. Here is my Code:
<Menu x:Name="MainMenu" Height="Auto" Width="Auto" VerticalAlignment="Top">
<MenuItem Header="_Menu" x:Name="Menu" Foreground="Black">
<MenuItem x:Name="RestartComputer" Header="_Restart Computer" Click="restart_Click"/>
<MenuItem x:Name="ShutdownComputer" Header="_Shutdown Computer" Click="shutdown_Click"/>
<MenuItem x:Name="OnTop" Header="_Always On Top" Click="OnTop_Click"/>
<Separator Width="Auto" Height="2"/>
<MenuItem x:Name="Exit" Header="_Exit" Click="MenuItem_Click"/>
</MenuItem>
<MenuItem x:Name="ControlPanel" Header="_Control Panel" Height="Auto" Width="Auto" Foreground="Black">
<MenuItem x:Name="Main" Header="Control Panel Main"/>
<MenuItem x:Name="ProgramsAndFeatures" Header="Programs and Features"/>
<MenuItem x:Name="AdministrativeTools" Header="Administrative Tools"/>
<MenuItem x:Name="DeviceManager" Header="Device Manager"/>
<MenuItem x:Name="NetworkAndSharing" Header="Network and Sharing"/>
<MenuItem x:Name="PowerOptions" Header="Power Options"/>
<Separator Width="Auto" Height="2"/>
<MenuItem x:Name="WindowsFirewall" Header="Windows Firewall"/>
<MenuItem x:Name="Display" Header="Display"/>
<Separator Width="Auto" Height="2"/>
<MenuItem x:Name="System" Header="System"/>
<MenuItem x:Name="DevicesAndPrinters" Header="Devices and Printers"/>
</MenuItem>
</Menu>
If i lessen these MenuItems under ControlPanel menu item to 4 or 5 no problem, but if i make them more than 5 or 6 i get the error what can it be ?
And also at MainWindow.g.cs line 316 is this.
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
Use different x:Name for
<MenuItem x:Name="System" Header="System"/>

Cascaded ContextMenu in WPF ComboBox

I want to create a ComboBox that lets users select items the way the Windows XP start menu allows to select programs:
Is there a way to insert a cascaded ContextMenu inside a ComboBox?
I tried this but it doesn't work properly:
<ComboBox>
<MenuItem Header="Top Level 1">
<MenuItem Header="Sub Level" />
<MenuItem Header="Sub Level" />
</MenuItem>
<MenuItem Header="Top Level 2">
<MenuItem Header="Sub Level" />
<MenuItem Header="Sub Level" />
</MenuItem>
</ComboBox>
You will want to try somthing like this, The next level must be inside the first level.
<Menu Margin="0,0,0,283">
<MenuItem Header="Top Level 1">
<MenuItem Header="Sub Level" />
<MenuItem Header="Sub Level" />
<MenuItem Header="Top Level 2">
<MenuItem Header="Sub Level" />
<MenuItem Header="Sub Level" />
<MenuItem Header="Top Level 3">
<MenuItem Header="Sub Level" />
<MenuItem Header="Sub Level" />
</MenuItem>
</MenuItem>
</MenuItem>
</Menu>
Do you have to use a ComboBox?
Using the Menu control will make your MenuItems work easily... so I would use that instead: http://wpftutorial.net/Menus.html
You can always alter the look of it as well. There are some examples here: http://www.c-sharpcorner.com/uploadfile/mahesh/menus-in-wpf/
Else, did you try to wrap your menuitems in a simply?

WPF find a nested menuitem in code

I have an image that has a nested context menu defined in the XAML (shown below).
I am trying to use a for loop to find a menu and uncheck it using IsChecked=false.
my code is as follows:
for (int i = 1; i <= 16; i++)
{
MenuItem theMenu = (MenuItem)this.FindName("beat" + i.ToString());
theMenu.IsChecked = false;
}
the above does not work and returns a null.
what am I doing wrong!
excerpt from the XAML
<Image x:Name="Options" Height="35" Source="Images/pad-options-button.png" Stretch="Fill" Width="47" Canvas.Left="740" Canvas.Top="293"
MouseUp="Options_MouseUp">
<Image.ContextMenu>
<ContextMenu>
<MenuItem Header="Beats">
<MenuItem Name="beat1" Header="1" Click="MenuBeats_Click"/>
<MenuItem Name="beat2" Header="2" Click="MenuBeats_Click"/>
<MenuItem Name="beat3" Header="3" Click="MenuBeats_Click"/>
<MenuItem Name="beat4" Header="4" Click="MenuBeats_Click"/>
<MenuItem Name="beat5" Header="5" Click="MenuBeats_Click"/>
<MenuItem Name="beat6" Header="6" Click="MenuBeats_Click"/>
<MenuItem Name="beat7" Header="7" Click="MenuBeats_Click"/>
<MenuItem Name="beat8" Header="8" Click="MenuBeats_Click"/>/>
<MenuItem Name="beat9" Header="9" Click="MenuBeats_Click"/>
<MenuItem Name="beat10" Header="10" Click="MenuBeats_Click"/>
<MenuItem Name="beat11" Header="11" Click="MenuBeats_Click"/>
<MenuItem Name="beat12" Header="12" Click="MenuBeats_Click"/>
<MenuItem Name="beat13" Header="13" Click="MenuBeats_Click"/>
<MenuItem Name="beat14" Header="14" Click="MenuBeats_Click"/>
<MenuItem Name="beat15" Header="15" Click="MenuBeats_Click"/>
<MenuItem Name="beat16" Header="16" Click="MenuBeats_Click"/>
</MenuItem>
<MenuItem Header="Beat Type">
<MenuItem Name="Whole" Header="Whole" Click="MenuBeatType_Click"/>
<MenuItem Name ="Half" Header="Half" Click="MenuBeatType_Click"/>
<MenuItem Name ="Quarter" Header="Quarter" Click="MenuBeatType_Click"/>
<MenuItem Name ="Eighth" Header="Eighth" Click="MenuBeatType_Click"/>
<MenuItem Name ="Sixteenth" Header="Sixteenth" Click="MenuBeatType_Click"/>
</MenuItem>
</ContextMenu>
</Image.ContextMenu>
</Image>
</Canvas>
An other option is to get the context menu from the image.
var items = Options.ContextMenu.Items
foreach(MenuItem item in items)
{
// do your work with the item
}
Be arware that you will have to handle the sub items!
Items will have only 2 items in your case
You have incorrect markup. Last two symbols shouldn't be there:
<MenuItem Name="beat8" Header="8" Click="MenuBeats_Click"/>/>
However, you example perfectly works for me, but with accidental item:
"/>"
Just in the constructor of the class in code behind write NameScope.SetNameScope(contextMenu, NameScope.GetNameScope(this)); contextMenu is the name given to context menu of image. Actually ContextMenu is not the part of Visual tree so it cant find the names . but the above line of code will set context menu in the scope of visual tree and hence will find the names. Hope this will help.

Categories

Resources