How would i display a context menu with a button in it by right-clicking on any element on form?
The purpose of a button in a form would be:
Displaying a x:Name of a control on which a right click(displaying a context menu )is performed.
To summarize, i want to right click on any element on form to display context menu with 1 button "Show me name" which should show messagebox displaying: "My name is [x:name of element]"
Define an implicit Style for each type of control:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300">
<Window.Resources>
<ContextMenu x:Key="cm">
<MenuItem Header="Show name" Click="MenuItem_Click" />
</ContextMenu>
<Style TargetType="Button">
<Setter Property="ContextMenu" Value="{StaticResource cm}" />
</Style>
<Style TargetType="TextBox">
<Setter Property="ContextMenu" Value="{StaticResource cm}" />
</Style>
</Window.Resources>
<StackPanel>
<Button x:Name="a" Content="a" />
<TextBox x:Name="b" />
</StackPanel>
</Window>
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
MenuItem mi = sender as MenuItem;
ContextMenu cm = mi.Parent as ContextMenu;
FrameworkElement fe = cm.PlacementTarget as FrameworkElement;
MessageBox.Show(fe.Name);
}
Related
I created a menuitem resource style that assists in showing a group of mutually exclusive selectable radio button items. When one of the radio button items is selected the menu will not close. I tried using StaysOpenOnClick but that doesn't seem to work. How can I get the menu to close when one of the radio button menu items is selected?
XAML:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="200">
<Grid>
<Menu>
<MenuItem Header="Menu" StaysOpenOnClick="False" >
<MenuItem.Resources>
<Style x:Key="GroupStyle1" TargetType="{x:Type RadioButton}">
<Setter Property="GroupName" Value="OptionGroup1"/>
</Style>
</MenuItem.Resources>
<MenuItem StaysOpenOnClick="False">
<MenuItem.Template>
<ControlTemplate>
<RadioButton
Content="Radio1"
Style="{StaticResource GroupStyle1}"/>
</ControlTemplate>
</MenuItem.Template>
</MenuItem>
<MenuItem StaysOpenOnClick="False">
<MenuItem.Template>
<ControlTemplate>
<RadioButton
Content="Radio2"
Style="{StaticResource GroupStyle1}"/>
</ControlTemplate>
</MenuItem.Template>
</MenuItem>
<MenuItem StaysOpenOnClick="False">
<MenuItem.Template>
<ControlTemplate>
<RadioButton
Content="Radio3"
Style="{StaticResource GroupStyle1}"/>
</ControlTemplate>
</MenuItem.Template>
</MenuItem>
</MenuItem>
</Menu>
</Grid>
I usually try not to use code behind with WPF, but for things like this, I think it may be justified. I'm sure if you think long enough you can write a whole bunch of xaml that can do this, but here's a quick and slightly dirty way of doing so:
Name the MenuItem
Add an event handler to the style, and use that to close the menu in the code behind. (See the EventSetter inside the style)
Xaml
<Grid>
<Menu>
<MenuItem x:Name="menuItem" Header="Menu">
<MenuItem.Resources>
<Style x:Key="GroupStyle1" TargetType="{x:Type RadioButton}">
<Setter Property="GroupName" Value="OptionGroup1"/>
<EventSetter Event="Checked" Handler="RadioButton_Checked"/> <!--Add this-->
</Style>
</MenuItem.Resources>
...
Code Behind
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
menuItem.IsSubmenuOpen = false;
}
I have the following Window:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen" ShowInTaskbar="True"
Style="{StaticResource BorderlessWindow}" SizeToContent="WidthAndHeight">
<Grid>
</Grid>
</Window>
And in my App.xaml I have the following BorderlessWindow Style:
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:shell="http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style x:Key="BorderlessWindow" TargetType="{x:Type Window}">
<Setter Property="MinWidth" Value="325" />
<Setter Property="MinHeight" Value="240" />
<Setter Property="shell:WindowChrome.WindowChrome">
<Setter.Value>
<shell:WindowChrome CaptionHeight="32"
GlassFrameThickness="0"
ResizeBorderThickness="12" />
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
</Application>
To get the application running, you need a reference to Microsoft.Windows.Shell. And to be complete: we are using .NET Framework 4.0 and VS 2015.
I have set the SizeToContent="WidthAndHeight" to get a smaller window, but when I do that I get a black "shade" to the right and bottom. With snoop I see that the MainWindow is set to the MinHeight and MinWidth, but the Border inside it isn't.
Why isn't the content resized as well? When the Window is updated by resizing the Window with the mouse the Window looks like expected.
You can add a handler for ContentRendered and force it to redraw once the content is rendered. You can hook it up in xaml or the code behind, here it is in code behind.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ContentRendered += OnContentRendered;
}
private void OnContentRendered(object sender, EventArgs eventArgs)
{
InvalidateVisual();
}
}
Im new at WPF and im using some DevExpress controls
Im trying to implement an style to some buttons but always shows the error that the Resource cannot be resolved.
MainWindow:
<dx:DXWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" x:Class="LicenceManagerWPF.MainWindow"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" dx:ThemeManager.ThemeName="Office2016"
Title="MainWindow" Height="746.218" Width="1139.154"
WindowStartupLocation="CenterScreen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80"/>
</Grid.RowDefinitions>
<StackPanel x:Name="Stack_Top" Orientation="Horizontal" Grid.Row="1" >
<dx:SimpleButton x:Name="btnRefresh" Style="{StaticResource ResourceKey=CustomStyles}" Width="55" ToolTip="Refresh" Margin="10,10,10,10" Glyph="{dx:DXImage Image=Refresh_32x32.png}" Content="Resfresh" />
<dx:SimpleButton x:Name="btndNew" Width="55" ToolTip="New Customer" Margin="10" Glyph="{dx:DXImage Image=NewContact_32x32.png}" Content="New Customer" />
<dx:SimpleButton x:Name="btnDelete" ToolTip="Delete Customer" Width="55" Margin="10" Content="Delete" Glyph="{dx:DXImage Image=Cancel_32x32.png}"/>
</StackPanel>
</Grid>
This is the App.xaml
<Application x:Class="LicenceManagerWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
Startup="OnAppStartup_UpdateThemeName">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Name="CustomStyles" Source="StyleResource.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
This is my Styles file
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LicenceManagerWPF"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" x:Class="LicenceManagerWPF.MainWindow"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core">
<Style x:Name="HeaderButtons" TargetType="dx:SimpleButton">
<Setter Property="Margin" Value="10"/>
<Setter Property="Width" Value="55"/>
</Style>
I was searching and everything looks like fine but i dont get it why it cannot be resolved.
Regards
There is no need to name ResourceDictionary , just provide its Source:
<ResourceDictionary Source="StyleResource.xaml"/>
ResourceDictionary items should have x:Key. In case of a Style if there is no explicit key, TargetType will be used as a key. It is a way to create default styles.
If you want named style, then set x:Key
<Style x:Key="HeaderButtons" TargetType="dx:SimpleButton">
<Setter Property="Margin" Value="10"/>
<Setter Property="Width" Value="55"/>
</Style>
And finally StaticResource extension references resources by resource key, not by names:
Style="{StaticResource HeaderButtons}"
Also: when you set a Style for a button, Margin and Width settings (Width="55" Margin="10,10,10,10") becomes redundant. They can be used to override style setting, but in this case they are the same so why write them?
I've a TabControl with this structure:
<TabControl>
<TabControl.Resources>
<Style TargetType="TabItem" BasedOn="{StaticResource MetroTabItem}">
<Setter Property="Controls:ControlsHelper.HeaderFontSize" Value="20" />
</Style>
</TabControl.Resources>
<local:Analysis />
</TabControl>
Now I need to split each TabItem, so I've create something like a UserControl with this structure:
<TabItem xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
mc:Ignorable="d" Header="Analysis" Style="{StaticResource MetroTabItem}">
<Grid>
</Grid>
the problem's that the TabItem imported with namespace local: doesn't take the style of the TabControl in the Resource.
So for solve this I need to put the style resource in each TabItem, this will cause redundancy code.
How can I take the style of TabControl Resource in the splitted TabItem?
Thanks.
You can create your own style as a static resource in your App.xaml or your Window.xaml like this:
<Application.Resources>
<Style TargetType="TabItem" x:Key="MyTabItemStyle" BasedOn="{StaticResource MetroTabItem}">
<Setter Property="Controls:ControlsHelper.HeaderFontSize" Value="20" />
</Style>
</Application.Resources>
You give your style a name (MyTabItemStyle in this example) which is based on the MetroTabItem style.
Then, instead of using the MetroTabItem style directly in your custom TabItem, you use your own style MyTabItemStyle like this:
<TabItem xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
mc:Ignorable="d" Header="Analysis" Style="{StaticResource MyTabItemStyle}">
<Grid>
</Grid>
</TabItem>
I've got a listView displaying a few text records. I need to increase the height of rows (working on a touch screen so I need thicker rows) without increasing the font size.
This is probably pretty trivial but I have no clue and can't find much on google.
Any help appreciated.
You can set the height of all ListViewItems in a ListView by using ItemContainerStyle:
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Height" Value="50" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
Or you could use styles to set it for all listviews. Here scoped to within a window:
<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style TargetType="ListViewItem">
<Setter Property="Height" Value="100"/>
</Style>
</Window.Resources>
...
</Window>
In XAML
<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<StackPanel>
<ListView x:Name="myListView">
<ListViewItem Height="50">Test</ListViewItem>
<ListViewItem Height="30">Test</ListViewItem>
</ListView>
</StackPanel>
</Grid>
</Window>
In C# Codebehind
foreach (ListViewItem lv in myListView.Items)
{
lv.Height = 30;
}
Hope you getting the Idea.