XAML menuitem that is visible but not selectable? - c#

So I'm extremely new to XAML and still learning. I've followed several tutorials on the menu and menuitems but I cannot seem to find a way to add something to a menu and not have it be selectable. I tried adding just a label and it still shows as selectable. I tried setting isenabled to false and it did turn grey, but was still selectable. I tried turning off hittest and that didn't work either. My guess is that this has something to do with setting up a custom style, which I know nothing about. I'm going to head back into the googles and dive into that, but in the mean time I wanted to post here to see if anyone could offer insight as to a good direction to head.
Edit:
Simple example that I tried:
<Menu>
<MenuItem Header="My Menu">
<Label Content="Label1"/> //Selectable even though it's a label. I would Expect a label to not be selectable.
<MenuItem Header="Menu Item 1"/> //Selectable as it should be.
<Separator /> //Not selectable
<MenuItem Header="Menu Item 2"/>
<Label Content="Another Label" IsEnabled="False"/> //Selectable even though it is set to IsEnabled=False.
</MenuItem>
</Menu>

This may work a bit more like what you want. I added Focusable="False" and IsHitTestVisible="False". Note I had to change the last <Label> to <MenuItem> since Focusable="False" didn't quite do it there.
<Menu>
<MenuItem Header="My Menu">
<Label Content="Label1"/>
<MenuItem Header="Menu Item 1" Focusable="False" IsHitTestVisible="False"/>
<Separator />
<MenuItem Header="Menu Item 2"/>
<MenuItem Header="Another Label" IsEnabled="False" Focusable="False" IsHitTestVisible="False"/>
</MenuItem>
</Menu>

Take a look at this article Menu Control Tutorial if you use commands the CanExecute method can grey out the item and make it non-selectable

Related

CheckBoxes "stealing" bitmaps from each other [duplicate]

I have a MenuItem like below
<MenuItem Header="Edit">
<MenuItem Header="Copy Direct Link" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageCommand}" />
<MenuItem Header="Copy Image Data" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageDataCommand}" />
<MenuItem Header="Paste" Icon="{StaticResource PasteIcon}" Command="{Binding PasteImageCommand}" />
</MenuItem>
Notice the 1st 2 items use the same icon, I get something like below
I tried removing the 2nd item,
<MenuItem Header="Edit">
<MenuItem Header="Copy Direct Link" InputGestureText="Ctrl+C" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageCommand}" />
<!--<MenuItem Header="Copy Image Data" InputGestureText="Ctrl+Alt+C" Icon="{StaticResource CopyIcon}" Command="{Binding CopyImageDataCommand}" />-->
<MenuItem Header="Paste" InputGestureText="Ctrl+P" Icon="{StaticResource PasteIcon}" Command="{Binding PasteImageCommand}" />
</MenuItem>
then I got something like
How can I reuse Icons?
See this question
An Image can only have one parent so it will be moved from the first MenuItem to the second. You can add the x:Shared attribute like this
<Window.Resources>
<Image x:Key="CopyIcon" x:Shared="False" Source="..." />
</Window.Resources>
From msdn
x:Shared Attribute
When set to false, modifies WPF
resource-retrieval behavior so that
requests for the attributed resource
create a new instance for each request
instead of sharing the same instance
for all requests.
You're most likely declaring CopyIcon as Image type in your resource, something like this:
<Window.Resources>
<Image x:Key="CopyIcon" Source="yourcopyicon.ico"/>
</Window.Resources>
So, the root cause of the problem is, Image is a visual element, since it derives from FrameworkElement (which is a visual element), and a visual element cannot have more than one parent at the same time. That is why the first MenuItem is not showing the icon, since the second MenuItem reset the parent of CopyIcon, making itself parent of the CopyIcon.
Hope this explanation is helpful to you. Now follow what Meleak has said in his response. :-)
Try the following:
<MenuItem Header=“Paste“ >
<MenuItem.Icon><Image Height=“16“ Width=“16“ Source=“paste.jpg“ /></MenuItem.Icon>
</MenuItem>

How to get rid of grey area on DockPanel and Calender?

I have a Calendar control made to drop down when needed, but I have these great big greay area on both sides.
I have tried everything I possibly can, but I can't seem to get rid of the grey area on both sides of a calendar.
This is the XAML,
<DockPanel Grid.Row="0" Grid.Column="8" Height="25">
<Menu DockPanel.Dock="Top" Height="25" Width="100">
<MenuItem Header="calender" Height="25" Width="100" >
<Calendar Name="CalenderSelect"
SelectionMode="MultipleRange"
SelectedDatesChanged="Calendar_OnSelectedDatesChanged"
Width="192">
</Calendar>
</MenuItem>
</Menu>
</DockPanel>
My second question, is this the correct way to create a drop down control with a calendar inside it. Or am I doing this wrong, any help would be much appreciated.
First of all, from my point of view to use a menu is a bad idea to create a drop down control. You can do something similar and easier with a button.
However if you want to use the MenuItem control, you have to change the MenuItem.ItemsTemplate in order to reduce margins and remove background color:
<Menu DockPanel.Dock="Top" Height="25" Width="100">
<MenuItem Header="calender" Height="25" Width="100" AutomationProperties.IsColumnHeader="True" >
<MenuItem.Items>
<Calendar Name="CalenderSelect" SelectionMode="MultipleRange" > </Calendar>
</MenuItem.Items>
<MenuItem.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Margin="-35,-5,-50,-5" Background="{x:Null}"></StackPanel>
</ItemsPanelTemplate>
</MenuItem.ItemsPanel>
</MenuItem>
</Menu>
I hope this can help you.

My menu showing up at the top left of the screen

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.

how to find out which child of a WPF container the contextmenu was opened for?

Because ToolbarTray does not support proper ItemsSource binding I'm doing things in code behind. Namely filling the tray. How can I tell which toolbar the contextmenu was opened for (with CommandParameter)?
<UserControl.Resources>
<ContextMenu x:Key="ToolbarContextMenu">
<MenuItem Header="Move to top" Command="{ui:CommandHandler MoveToTop}" />
<MenuItem Header="Move to left" Command="{ui:CommandHandler MoveToLeft}" />
<MenuItem Header="Make float" Command="{ui:CommandHandler MakeFloat}" />
</ContextMenu>
</UserControl.Resources>
<ToolBarTray x:Name="MainToolbarTray" Orientation="{Binding Orientation}" ContextMenu="{StaticResource ToolbarContextMenu}"/>
In example above I use a toolkit where commands in ViewModel are decorated with attribute and in xaml command can be defined like this.
(Not the same as suggested. I don't want to find ToolbarTray, but the Toolbar)
EDIT: I just assigned the ContextMenu to Toolbar in code behind and used PlacementTarget as suggested.

Adding drop shadow on a menu item box

This is my first attempt at creating an application, so apologies if my questions are misformed or misplaced.
When you click on 'file' in a menu bar and a little box appears below with options like 'quit' and 'save,' how do I put a drop shadow on this box? I've seriously been tinkering and googling for the longest time with no luck. I feel like I've exhausted the options in visual studio and tried various code in my xaml. Here's the relevant XAML:
<Menu x:Name="MenuBar" HorizontalAlignment="Stretch" Height="27" Margin="-20,-36,140.4,0" VerticalAlignment="Top" Width="Auto" Foreground="Black" RenderTransformOrigin="0.5,0" Background="#FFE5E5E5">
<MenuItem x:Name="File" Header="File" Height="27" Width="39" TextOptions.TextHintingMode="Animated" Foreground="#FF323334" BorderThickness="0" BorderBrush="{x:Null}" Margin="1,0,4,4" Padding="6,0">
<MenuItem x:Name="Save" Header="Save" HorizontalAlignment="Left" Width="139.2" Click="Save_Click"/>
<MenuItem Header="Quit" HorizontalAlignment="Left" Width="139.2" Click="MenuItem_Click" Background="{x:Null}" BorderBrush="{x:Null}" UseLayoutRounding="False"/>
</MenuItem>
</Menu>
Thanks in advance for any help!
EDIT: I ended up getting my answer eslewhere
You just need to create your own Template
here's a link on how to ovveride the default MenuItem Style

Categories

Resources