我不明白为什么当所有其他绑定看起来都工作时,我的commandparameter为null,显示正确的图像,文本正确并调用命令CanExecute,但由于某种原因,参数为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>
和视图模型类:
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; }
}
并且命令的CanExecute如下所示:
public bool CanExecute(object parameter)
{
return parameter != null && parameter is string; // <---parameter is null
}