Click here to Skip to main content
15,559,567 members
Home / Discussions / WPF
   

WPF

 
QuestionHow to implement a gallery like the built-in gallery in Windows Phone Pin
small25-Sep-12 17:22
small25-Sep-12 17:22 
GeneralRe: How to implement a gallery like the built-in gallery in Windows Phone Pin
small29-Sep-12 16:19
small29-Sep-12 16:19 
QuestionSet background of TreeViewItem within HierarchicalDataTemplate with converter Pin
toengi19824-Sep-12 21:19
toengi19824-Sep-12 21:19 
AnswerRe: Set background of TreeViewItem within HierarchicalDataTemplate with converter Pin
Wayne Gaylard4-Sep-12 22:57
professionalWayne Gaylard4-Sep-12 22:57 
SuggestionRe: Set background of TreeViewItem within HierarchicalDataTemplate with converter Pin
toengi19824-Sep-12 23:03
toengi19824-Sep-12 23:03 
GeneralRe: Set background of TreeViewItem within HierarchicalDataTemplate with converter Pin
Wayne Gaylard4-Sep-12 23:11
professionalWayne Gaylard4-Sep-12 23:11 
SuggestionRe: Set background of TreeViewItem within HierarchicalDataTemplate with converter Pin
Mycroft Holmes5-Sep-12 13:52
professionalMycroft Holmes5-Sep-12 13:52 
QuestionCommandParameter is null when opening a ContextMenu Pin
AlexZieg713-Sep-12 23:57
AlexZieg713-Sep-12 23:57 
Hi,

I have found a confusing behavior in ContextMenu when processing bound Commands. I want to bind/define a CommandParameter by ItemContainerStyle:

XML
<ContextMenu>
  <MenuItem Command="{Binding MyCommand}" />
  <ContextMenu.ItemContainerStyle>
    <Style TargetType="MenuItem">
      <Setter Property="Header" Value="Item" />
      <Setter Property="CommandParameter" Value="ValidParameter" />
    </Style>
  </ContextMenu.ItemContainerStyle>
</ContextMenu>


When the menu opens, the CanExecute is called with a command parameter == null! However it is called with the expected parameter immediately before executing the command. But as I want to have the CanExecute return value to depend on the command parameter (enable or disable the menu item), I need the correct parameter also when opening the menu.

Following is a test application to show the bad behavior (see program output for the passed parameter). I would be happy if somebody could help me. By the way, when I directly bind the parameter in MenuItem, everything works fine, but I don't want this because I want to dynamically build the menu using MVVM later.

Thanks!


XAML:
XML
<Window x:Class="CommandParameterExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">

  <StackPanel>
    <Border Margin="4" Height="50" Background="Green">
      <Border.ContextMenu>
        <ContextMenu>
          <MenuItem Command="{Binding MyCommand}" />
          <ContextMenu.ItemContainerStyle>
            <Style TargetType="MenuItem">
              <Setter Property="Header" Value="Item" />
              <Setter Property="CommandParameter" Value="ValidParameter" />
            </Style>
          </ContextMenu.ItemContainerStyle>
        </ContextMenu>
      </Border.ContextMenu>
    </Border>
  </StackPanel>
</Window>


Code-Behind:
C#
using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Input;

namespace CommandParameterExample
{
    public partial class MainWindow
    {
        public ICommand MyCommand { get; private set; }

        public MainWindow()
        {
            MyCommand = new CommandModel();
            InitializeComponent();
        }
    }

    internal class CommandModel : ICommand
    {
        public bool CanExecute(object parameter)
        {
            WriteParameter(parameter, "CanExecute");
            return true;
        }

        public void Execute(object parameter)
        {
            WriteParameter(parameter, "Execute");
        }

        private static void WriteParameter(object parameter, string context)
        {
            Debug.WriteLine("Parameter in {0}: {1}", context, parameter ?? "null");
        }

        public event EventHandler CanExecuteChanged;
    }
}


modified 4-Sep-12 5:07am.

QuestionReuse template for different listbox items Pin
LionAM3-Sep-12 6:09
LionAM3-Sep-12 6:09 
Questiontreeview that has a custom treeview that consists of a button and a textbox Pin
truel3-Sep-12 5:48
truel3-Sep-12 5:48 
AnswerRe: treeview that has a custom treeview that consists of a button and a textbox Pin
Abhinav S3-Sep-12 8:12
Abhinav S3-Sep-12 8:12 
QuestionXML file source from users/appdata/roaming folder Pin
Jayme653-Sep-12 4:31
Jayme653-Sep-12 4:31 
AnswerRe: XML file source from users/appdata/roaming folder Pin
Mycroft Holmes3-Sep-12 14:11
professionalMycroft Holmes3-Sep-12 14:11 
GeneralRe: XML file source from users/appdata/roaming folder Pin
Pete O'Hanlon3-Sep-12 23:18
mvaPete O'Hanlon3-Sep-12 23:18 
GeneralRe: XML file source from users/appdata/roaming folder Pin
Mycroft Holmes4-Sep-12 1:34
professionalMycroft Holmes4-Sep-12 1:34 
QuestionSilverlight-Font Pin
vickypriya2-Sep-12 20:20
vickypriya2-Sep-12 20:20 
AnswerRe: Silverlight-Font Pin
delibey6-Sep-12 10:28
delibey6-Sep-12 10:28 
QuestionRedirect from silverlight to web page with values Pin
Anupbala29-Aug-12 22:13
Anupbala29-Aug-12 22:13 
AnswerRe: Redirect from silverlight to web page with values Pin
Christian Amado30-Aug-12 8:35
professionalChristian Amado30-Aug-12 8:35 
GeneralRe: Redirect from silverlight to web page with values Pin
Anupbala31-Aug-12 21:54
Anupbala31-Aug-12 21:54 
GeneralRe: Redirect from silverlight to web page with values Pin
Pete O'Hanlon31-Aug-12 22:56
mvaPete O'Hanlon31-Aug-12 22:56 
GeneralRe: Redirect from silverlight to web page with values Pin
Anupbala31-Aug-12 22:57
Anupbala31-Aug-12 22:57 
QuestionPDF Flipbook Pin
defjef29-Aug-12 0:51
defjef29-Aug-12 0:51 
AnswerRe: PDF Flipbook Pin
Pete O'Hanlon29-Aug-12 1:03
mvaPete O'Hanlon29-Aug-12 1:03 
QuestionBin\Debug\AppManifest.xaml Error Pin
er.ravindra28-Aug-12 2:07
professionaler.ravindra28-Aug-12 2:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.