Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Alright, this solution works: I changed the WindowV.xaml and WindowV.xaml.cs files as follows. The following corrections fix the problem concerning namescope in xaml.</p> <p>Here's the new WindowV.xaml file:</p> <pre><code>&lt;Window x:Class="WpfApplication1.WindowV" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src="clr-namespace:WpfApplication1" Name="RootElement" Title="WindowV" Height="300" Width="300"&gt; &lt;Window.Resources&gt; &lt;ContextMenu x:Key="myContextMenu" DataContext="{Binding Path=DataContext, ElementName=RootElement}"&gt; &lt;MenuItem Header="{Binding MenuItem1}" /&gt; &lt;MenuItem Header="{Binding MenuItem2}" /&gt; &lt;MenuItem Header="{Binding MenuItem3}" /&gt; &lt;/ContextMenu&gt; &lt;Style TargetType="{x:Type ItemsControl}"&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding IsLocked}" Value="true"&gt; &lt;Setter Property="ItemsSource" Value="{Binding LockedList}" /&gt; &lt;Setter Property="ItemTemplate"&gt; &lt;Setter.Value&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding}" /&gt; &lt;/DataTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/DataTrigger&gt; &lt;DataTrigger Binding="{Binding IsLocked}" Value="false"&gt; &lt;Setter Property="ItemsSource" Value="{Binding RegularList}" /&gt; &lt;Setter Property="ItemTemplate"&gt; &lt;Setter.Value&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding}" ContextMenu="{StaticResource myContextMenu}" /&gt; &lt;/DataTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="4*" /&gt; &lt;RowDefinition Height="*" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;ItemsControl Grid.Row="0" /&gt; &lt;Button Name="ToggleButton" Grid.Row="1" Content="Toggle Lock" Click="OnToggleLock" /&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>Here's the corresponding code-behind:</p> <pre><code>using System.Windows; using System.Windows.Markup; using System; namespace WpfApplication1 { /// &lt;summary&gt; /// Interaction logic for Window1.xaml /// &lt;/summary&gt; public partial class WindowV : Window { public WindowV() { InitializeComponent(); System.Windows.Controls.ContextMenu contextMenu = FindResource("myContextMenu") as System.Windows.Controls.ContextMenu; NameScope.SetNameScope(contextMenu, NameScope.GetNameScope(this as DependencyObject)); contextMenu.RegisterName("RootElement", this); } private void OnToggleLock(object sender, RoutedEventArgs e) { if (((WindowVM)(DataContext)).IsLocked == true) ((WindowVM)(DataContext)).IsLocked = false; else ((WindowVM)(DataContext)).IsLocked = true; } } } </code></pre> <p>Andrew</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload