0

I don't understand why my commandparameter is null when all of the rest of the bindings seem to work, the correct image is displayed, the text is correct and the command CanExecute is called, but for some reason the parameter is null.

                <MenuItem Header="Open Recent" ItemsSource="{Binding Path=MRU}">
                <MenuItem.ItemContainerStyle>
                    <Style TargetType="MenuItem">
                        <Setter Property="Command" Value="{Binding Command}" />
                        <Setter Property="CommandParameter" Value="{Binding URI}" />
                        <Setter Property="Header" Value="{Binding URI}" />
                        <Setter Property="Icon">
                            <Setter.Value>
                                <Image Source="{Binding URIImage}" />
                            </Setter.Value>
                        </Setter>
                    </Style>
                </MenuItem.ItemContainerStyle>
            </MenuItem>

and the view model class:

        public class MRU_ViewModel
    {

        public MRU_ViewModel(string uri)
        {
            this.URI = uri;
            Command = new Commands.Open();
        }


        public string URI { get; private set; }
        public System.Windows.Media.Imaging.BitmapImage URIImage { get { return WorkSpace.GetURIImage(URI); } }
        public ICommand Command { get; private set; }
    }

and the CanExecute of the command looks like this:

        public bool CanExecute(object parameter)
    {
        return parameter != null && parameter is string; // <---parameter is null
    }
Matthew Sanford
  • 1,069
  • 1
  • 13
  • 21
  • Have you tried finding Ancestor in your Bindings? And I take it that your header is displayed correctly ? – XAMlMAX Feb 11 '15 at 11:58
  • Yes, header is displayed correctly as is the image, and the command is executed, but the parameter is null, which I know cannot be right, because the Header is displaying properly. – Matthew Sanford Feb 11 '15 at 15:05
  • Use Header then i.e. `{Binding Path=Header, RelativeSource={RelativeSource Self}}`. HTH – XAMlMAX Feb 11 '15 at 15:54
  • I did find a workaround ie, I just pass the url into the constructor of the Save command and look at that if the parameter is null, but now I am curious as to why this method does not work. – Matthew Sanford Feb 11 '15 at 19:20
  • This is a known issue http://stackoverflow.com/questions/20670870/wpf-menuitem-commandparameter-binding-set-to-null – Matthew Sanford Feb 12 '15 at 06:13

0 Answers0