Reputation: 3461
The problem I am facing is a too big XAML file. I want to separate things into resources.
The Header
of the MenuItem is rendered without text, and it is neither working with x:Reference
and Binding.Source
instead of Binding.ElementName
.
If I give the Header
property below a local value, it works. I need this context menu available in this resource dictionary because I use it in many other big resources (especially big DataTemplate
s in the same resource dictionary.
If I put this ContextMenu
, and all the resources using it from Window.Resources
into ControlTemplate.Resources
, everything works, but I would like to separate them for better organization because they are big.
I have searched Stack Overflow for duplicate questions but I did not find something similar to my situation. I thought about it for a while and I did not find a tehnique that helps me. (I have read only about 80% of Pro WPF 4.5.)
(there is no code-behind other than that autogenerated code)
<Window x:Class="wpf_test_1.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:wpf_test_1"
mc:Ignorable="d"
Title="MainWindow" Height="100" Width="200">
<Window.Resources>
<ContextMenu x:Key="MyContextMenu">
<MenuItem Header="{Binding ElementName=MyTextBlock,
Path=Text}"/>
</ContextMenu>
</Window.Resources>
<Grid ContextMenu="{StaticResource MyContextMenu}">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button>
A button
</Button>
<Button Grid.Row="1">
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="MyTextBlock" Text="Test"/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
</Window>
Actual: The context menu of the first/root Grid
has a single MenuItem
that has an empty Header
. This can be checked by right clicking this Grid
.
Expected: It should have the Header
set to "Test".
Thank you.
The ContextMenu in my actual project begins like this:
<ContextMenu x:Key="MyContextMenu">
<MenuItem Click="ApplyFilters_Click"
Visibility="{Binding ElementName=MyListView,
Path=SelectedItems.Count,
Converter={StaticResource MultipleSelectedToVisibleConv}}"
Header="_Apply filters">
</MenuItem>
MyContextMenu
is shown when the user right-clicks any element in the MyListView
. For MyListView I have 4 combinations of DataTemplates, GridView and ItemsPanelTemplate that offer the user 4 ways to view the information. Each DataTemplate references the MyContextMenu so it is shown when the user right-clicks an item.
I meant "right-clicks any item in MyListView".
<DataTemplate x:Key="LargeIconsWithViewboxTemplate">
<Viewbox>
<Grid Tag="{Binding Path=MyFilter}"
ContextMenu="{StaticResource MyContextMenu}"
Width="84" Height="60" ToolTip="{Binding Path=DisplayString}">
[...]
Upvotes: 0
Views: 777