Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This would be your xaml:</p> <pre><code>&lt;Window x:Class="MyWpfApp.MyWindow" xmlns:cmd="clr-namespace:MyWpfApp.MyCommandsNamespace" xmlns:vm="clr-namespace:MyWpfApp.MyViewModelsNamespace" ...&gt; &lt;Window.Resources&gt; &lt;DataTemplate x:Key="MyItemTemplate" DataType="{x:Type vm:MyItemClass}"&gt; &lt;TextBlock Text="{Binding MyItemText}"/&gt; &lt;/DataTemplate&gt; &lt;/Window.Resources&gt; &lt;Window.CommandBindings&gt; &lt;CommandBinding Command="{x:Static cmd:MyCommandsClass.MyCommand1}" Executed="ExecuteMyCommand" CanExecute="CanExecuteMyCommand"/&gt; &lt;/Window.CommandBindings&gt; &lt;Window.ContextMenu&gt; &lt;ContextMenu&gt; &lt;MenuItem Header="MyMenuItem1" CommandTarget="{Binding}" Command="{x:Static cmd:MyCommandsClass.MyCommand1}"/&gt; &lt;/ContextMenu&gt; &lt;/Window.ContextMenu&gt; &lt;Grid&gt; &lt;ItemsControl ItemsSource="{Binding MyList}" ItemTemplate="{StaticResource MyItemTemplate}"/&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>and this would be your cs code:</p> <pre><code> public MyWindow() { VM = new MyViewModelsNamespace.MyViewModel(); this.DataContext = VM; InitializeComponent(); } public void ExecuteMyCommand(object sender, ExecutedRoutedEventArgs e) { VM.MyList.Add(new MyItemClass{MyItemText="some text"}); } public void CanExecuteMyCommand(object sender, CanExecuteRoutedEventArgs e) { if (...) e.CanExecute = false; else e.CanExecute = true; } </code></pre> <p>where MyViewModel is something like:</p> <pre><code> public class MyViewModel : DependencyObject { //MyList Observable Collection private ObservableCollection&lt;MyItemClass&gt; _myList = new ObservableCollection&lt;MyItemClass&gt;(); public ObservableCollection&lt;MyItemClass&gt; MyList { get { return _myList; } } } </code></pre> <p>and MyItemClass is something like:</p> <pre><code> public class MyItemClass : DependencyObject { //MyItemText Dependency Property public string MyItemText { get { return (string)GetValue(MyItemTextProperty); } set { SetValue(MyItemTextProperty, value); } } public static readonly DependencyProperty MyItemTextProperty = DependencyProperty.Register("MyItemText", typeof(string), typeof(MyItemClass), new UIPropertyMetadata("---")); } </code></pre> <p>I forgot to mention command:</p> <pre><code>public static class MyCommandsClass { public static RoutedCommand MyCommand1 = new RoutedCommand(); } </code></pre>
    singulars
    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.
    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