Changing ContextMenu Appearance - c#

currently I have the basic radgridview Context menu, I'm trying to get it to look like the context menu in here from this demo ContextMenu
May I get some tips or suggestions, I'm not quite sure where to start.
My Contextmenu code:
<telerik:RadGridView.ContextMenu>
<ContextMenu>
<MenuItem Header="View Contact" cal:Message.Attach="[Event Click] = [Action Open()]"/>
</ContextMenu>
</telerik:RadGridView.ContextMenu>

Since you are asking for a direction here.
I would suggest you to read about Styles and templates in WPF (MSDN)
In WPF the controls are view-less. The view is controlled by the style/template that you apply to the control.
For instance, in your case if you can get the style of the Context menu control you have mentioned (say it's named "FancyContextMenu" in resource dictionary).
Then you just need to modify your code to below:
<telerik:RadGridView.ContextMenu>
<ContextMenu Style="{StaticResource ResourceKey=FancyContextMenu}">....

Related

1 Control, two separate binding sources

Good Morning!
I have a WPF application that will display a number of different file types based on command line args it receives. It works fine, but I want to go back and refactor it. I have only been a developer for a few years and would like to master MVVM.
I am using an MVVM design package called Stylet. In my PDF view I am using a Telerik RadPdfViewer control to which Telerik has all this binding stuff built in for you. For example, I am binding the right click context menu with the commands "select all" and "copy" using their pre configured command bindings.
I would like to bind the "Document Source" property TO MY viewmodel so I can pass in the paths of documents I want to load. However, the DataContext of the control is bound to Telerik's CommandDescriptors preventing the binding to my viewmodel.
<telerik:RadPdfViewer x:Name="radPdfViewer" Grid.Row="1"
DataContext="{Binding CommandDescriptors, ElementName=radPdfViewer}"
DocumentSource="{Binding PDFDoc}"
telerik:RadPdfViewerAttachedComponents.RegisterFindDialog="True"
HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch"
telerik:StyleManager.Theme="Office_Black" Grid.ColumnSpan="2">
<telerik:RadContextMenu.ContextMenu>
<telerik:RadContextMenu>
<telerik:RadMenuItem Header="Select All"
Command="{Binding SelectAllCommandDescriptor.Command}" />
<telerik:RadMenuItem Header="Copy"
Command="{Binding CopyCommandDescriptor.Command}" />
</telerik:RadContextMenu>
</telerik:RadContextMenu.ContextMenu>
</telerik:RadPdfViewer>
public class PDFViewModel
{
private string _pdfDoc;
public string PDFDoc
{
get
{
return _pdfDoc;
}
set
{
_pdfDoc = value;
}
}
public PDFViewModel()
{
PDFDoc = #"t:\share\large.pdf";
}
}
I see two choices
I break Telerik's prebuilt command bindings and figure out how to bring the select all and copy functions to my viewmodel.
Stylet has an s:Action function where I can call a method where I can load the document into the RadPdfViewer control using C#. I would need to somehow get control of the gui control in the method of my viewmodel and I am not sure how to do that.
Is there a better way? A little nudge in the right direction would be greatly appreciated.
Jason Tyler's reply got me going in the right direction. Thank you!
So because I am using a ViewModel first pattern, I did not need to specify the DataContext of the user control like I thought...Its already set.
However, his suggestion of binding using the relative source and researching on how to do this (I have never used RelativeSource before..I am kinda new to this stuff) I came across this Stack post
How do I use WPF bindings with RelativeSource?
A Jeff Knight Posted a diagram of how ancestor binding works.
Using that, I was able to figure out the syntax and my document came right up and I can still use the right click context menu items that are bound to Telerik. So now my Xaml looks like this note how the Document source binding has changed.
<telerik:RadPdfViewer x:Name="radPdfViewer" Grid.Row="1"
DataContext="{Binding CommandDescriptors, ElementName=radPdfViewer}"
DocumentSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.PDFDoc}"
telerik:RadPdfViewerAttachedComponents.RegisterFindDialog="True"
HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch"
telerik:StyleManager.Theme="Office_Black" Grid.ColumnSpan="2">
<telerik:RadContextMenu.ContextMenu>
<telerik:RadContextMenu>
<telerik:RadMenuItem Header="Select All"
Command="{Binding SelectAllCommandDescriptor.Command}" />
<telerik:RadMenuItem Header="Copy"
Command="{Binding CopyCommandDescriptor.Command}" />
</telerik:RadContextMenu>
</telerik:RadContextMenu.ContextMenu>
</telerik:RadPdfViewer>

Binding a ContextMenu item's "IsEnabled" to a Button

Is it possible to bind the IsEnabled property of a button to a context menu item that is represented by a NotifyIcon?
When I push the menu item, it starts a method that disables my btnSave. In that case, I would like to "turn off" the MenuItem too. I attempted it this way, but it's not working:
<Window.Resources>
<ContextMenu x:Key="NotifierContextMenu" Placement="MousePoint">
<MenuItem Header="Start" Click="start_timer" IsEnabled="{Binding ElementName=btnSave, Path=IsEnabled}"/>
</ContextMenu>
</Window.Resources>
I believe your problem stems from a ContextMenu not existing within the visual tree, so ElementName bindings will be unsuccessful, unless you take some additional steps to correct the Name Scope, or resolve the DataContext to the visual tree.
Most of the techniques are covered fairly comprehensively in this answer: ElementName Binding from MenuItem in ContextMenu.
I've had success in the past with binding to a named element by reference, in your case, that would look something like this:
<MenuItem Header="Start"
IsEnabled="{Binding IsEnabled, Source={x:Reference btnSave}}"
Click="btnSave_Click"/>

How to pass an index of a selected context menu item to a bound command?

How could i bind all context menu items to one single command, which gets the index of the menu item called? I have a WPF/MVVM project.
<ContextMenu x:Key="cm" ItemsSource="{Binding ActionItems}"
DisplayMemberPath="ActionDescription">
</ContextMenu>
Your should pass it as a CommandParameter:
<ContextMenu x:Key="cm" ItemsSource="{Binding ActionItems}" DisplayMemberPath="ActionDescription"
Command="{Binding YourCommand}" CommandParameter="{Binding YourParameter}">
...
</ContextMenu>
Updated
you need the solution described here: MVVM binding command to contextmenu item
Updated after comments
relapse - than you should implement it as shown here: WPF ContextMenu with ItemsSource - how to bind to Command in each item?. Please notice that link is a duplication of other question. So read the both please.

ContextMenu datacontext binding

Heres a simple question. I've got a XAML that at the moment looks like this:
<ListBox>
<ListBox.ContextMenu>
<ContextMenu DataContext="{Binding Path=FeedContextMenu}"
</ListBox.ContextMenu>
</ListBox>
My intention is to take the data context from the ListBox and use its FeedContextMenu property as a DataContext for ContextMenu. Now all you WPF gurus will probably immediately say that this will not work. Apparently, this has something to do with the fact that ContextMenu isn't part of the visual tree. Now I'm not a WPF expert, so after googling for hours and trying out different suggested solutions that didn't work nor make any sense to me whatsoever, I would like to ask someone with a greater knowledge to explain it to me what and why needs to be done in order for this to work. Thanks.
You could try to bind to the DataContext of the ContextMenu's PlacementTarget (which is the ListBox) like this:
<ContextMenu DataContext="{Binding PlacementTarget.DataContext.FeedContextMenu,
RelativeSource={RelativeSource Self}}">

Drop Down List with WPF Menu Controls

I am looking for a way to add a drop down list in WPF to a menu. This used to be really easy in winforms and so I am expecting you experts to know just now to do it in WPF. Thanks.
Sorry if this is a bad question, it is late and I don't want to think.
It is very easy to add any UIElement to any control, You can just add Combobox to a Menu control and create menu as bellow.
<Menu>
<MenuItem Header="File">
<MenuItem Header="Open"/>
<MenuItem Header="Close"/>
<Separator/>
<ComboBox Width="85" Height="21.96" />
</MenuItem>
</Menu>
While this is very easy to do as Jobi Joy has shown, I think it has horrible usability. The Menu control supports multiple levels of menu items and I would go down that route for UI consistency.

Categories