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"/>
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 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.
I am try do a menu like the photo below:
I have this code:
<Menu >
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</Menu.ItemsPanel>
<MenuItem Header="Item1">
<MenuItem Header="SubItem 1">
<MenuItem Header="SubItem 1.1"></MenuItem>
<MenuItem Header="SubItem 1.2"></MenuItem>
<MenuItem Header="SubItem 1.3"></MenuItem>
<MenuItem Header="SubItem 1.4"></MenuItem>
</MenuItem>
<MenuItem Header="SubItem 2"></MenuItem>
<MenuItem Header="SubItem 3"></MenuItem>
<MenuItem Header="SubItem 4"></MenuItem>
</MenuItem>
<MenuItem Header="Item2">
<MenuItem Header="SubItem 1"></MenuItem>
<MenuItem Header="SubItem 2"></MenuItem>
<MenuItem Header="SubItem 3"></MenuItem>
</MenuItem>
</Menu>
but this code returns a menu like in the pictures below:
First appears this:
and when I put the mouse hover the Item 1 appears like this:
I want to that the second level of the Menu open on the right side of the first, like in the first image.
You have to create a custom template for your Menu to achieve your goal.
Here is the default ControlTemplate for Menu, I think the easiest way is to start from that. You will have to add HorizontalOffset and VerticalOffset for the PopUp in the TopLevelHeader template, so you can align it to your needs (or simply set Placement to Right - which is easier in my opinion).
Also, you should set a Width for your menu (either directly or by placing it inside some container that restricts it's Width), otherwise it will take up all the space and the PopUp might not be visible .
I will not replicate the whole XAML here, but here is the important part:
// ...
<!-- TopLevelHeader -->
<ControlTemplate x:Key="{x:Static MenuItem.TopLevelHeaderTemplateKey}"
TargetType="MenuItem">
<Border Name="Border" >
<Grid>
<ContentPresenter
Margin="6,3,6,3"
ContentSource="Header"
RecognizesAccessKey="True" />
<Popup
Name="Popup"
Placement="Right" <!-- This is modified -->
IsOpen="{TemplateBinding IsSubmenuOpen}"
AllowsTransparency="True"
Focusable="False"
PopupAnimation="Fade">
// ... (You need all the XAML from the linked MSDN site in your Resources somewhere)
After that you can use your Menu almost exactly like you did (I only added Width):
<Menu Width="300">
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</Menu.ItemsPanel>
<MenuItem Header="Item1">
<MenuItem Header="SubItem 1">
<MenuItem Header="SubItem 1.1"></MenuItem>
<MenuItem Header="SubItem 1.2"></MenuItem>
<MenuItem Header="SubItem 1.3"></MenuItem>
<MenuItem Header="SubItem 1.4"></MenuItem>
</MenuItem>
<MenuItem Header="SubItem 2"></MenuItem>
<MenuItem Header="SubItem 3"></MenuItem>
<MenuItem Header="SubItem 4"></MenuItem>
</MenuItem>
<MenuItem Header="Item2">
<MenuItem Header="SubItem 1"></MenuItem>
<MenuItem Header="SubItem 2"></MenuItem>
<MenuItem Header="SubItem 3"></MenuItem>
</MenuItem>
</Menu>
You might want to name the template and apply it to this Menu directly, not to mess up other Menus in your app...
The result:
Of course some more styling is needed to get the exact result you want, but I hope you got the idea.
I am implementing a texteditor in WPF. I am pretty new to WPF and C#. I am able to get tooltip for "Cut","Copy","Paste" options. But how do I get tooltip for File operations like "New","Open","Save","Save_As" and "Close"? Also I do see that when I edit a file in my Richtextbox, the editor does not indicate that file is changed. How do I implement this feature? Any help is appreciated.
<MenuItem Header="_File">
<MenuItem Header="_New" Click="New_Click"/>
<Separator />
<MenuItem Header="_Open" Click="Open_Click"/>
<Separator />
<MenuItem Header="_Save" Click="Save_Click">
<MenuItem.Icon>
<Image Source="C:\Users\stambi\Documents\Visual Studio 2012\Projects\PatternEditor\PatternEditor\Images\FileSave.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="_Save As" Click="Save_As_Click">
<MenuItem.Icon>
<Image Source="C:\Users\stambi\Documents\Visual Studio 2012\Projects\PatternEditor\PatternEditor\Images\FileSaveAs.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
<Separator />
<MenuItem Header="_Close" Click="Close_Click"/>
<Separator />
<MenuItem Header="_Exit" Click="Exit_Click"/>
</MenuItem>
<MenuItem Header="_Edit">
<MenuItem Header="_Cut" Command="ApplicationCommands.Cut" ToolTip="Cut">
<MenuItem.Icon>
<Image Source="C:\Users\stambi\Documents\Visual Studio 2012\Projects\PatternEditor\PatternEditor\Images\EditCut.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="_Copy" Command="ApplicationCommands.Copy" ToolTip="Copy">
<MenuItem.Icon>
<Image Source="C:\Users\stambi\Documents\Visual Studio 2012\Projects\PatternEditor\PatternEditor\Images\EditCopy.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="_Paste" Command="ApplicationCommands.Paste" ToolTip="Paste">
<MenuItem.Icon>
<Image Source="C:\Users\stambi\Documents\Visual Studio 2012\Projects\PatternEditor\PatternEditor\Images\EditPaste.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="_Undo" Command="ApplicationCommands.Undo" ToolTip="Undo">
<MenuItem.Icon>
<Image Source="C:\Users\stambi\Documents\Visual Studio 2012\Projects\PatternEditor\PatternEditor\Images\EditUndo.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="_Redo" Command="ApplicationCommands.Redo" ToolTip="Redo">
<MenuItem.Icon>
<Image Source="C:\Users\stambi\Documents\Visual Studio 2012\Projects\PatternEditor\PatternEditor\Images\EditRedo.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
</MenuItem>
You do the same thing with New, Open, Save you have to define the ToolTip property.
Regarding how to determine if the file has changed depends on how you want to build it. Assuming you have Dock Windows as the parent of the RichTextBox Do you want an indicator that gives a * in the Header of the DockWindow or do you just want to know if the file has changed then you can use an event called TextChanged.
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.