ContextMenu中的CommandParameter返回null

时间:2019-06-19 07:39:22

标签: wpf vb.net xaml

我试图借助ContextMenu在DataGrid中对所选项目执行一些操作。我正在使用命令和命令参数。问题是命令参数返回null且程序崩溃了,然后才可以从网格中实际选择一个项目。 这是我的代码

function wpb_list_child_pages()
{
  global $post;

  if(is_page() && $post->post_parent)
    $childpages=wp_list_pages('sort_column=menu_order&title_li=&child_of='.$post->post_parent.'&echo=0');
  else
    $childpages=wp_list_pages('sort_column=menu_order&title_li=&child_of='.$post->ID.'&echo=0');

  if($childpages)
    $string='<ul class="list-unstyled">'.$childpages.'</ul>';

  return $string;
}

add_shortcode('wpb_childpages', 'wpb_list_child_pages');

viewModel类:

<Window.Resources>
    <local:DataViewModel x:Key="dvm"/>
</Window.Resources>
<DataGrid.ContextMenu>
                <ContextMenu x:Name="contextMenu">
                    <MenuItem Header="Suggest sollution" Command="{Binding 
SuggestSollution, Source={StaticResource dvm}}" CommandParameter="{Binding 
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type 
ContextMenu}}, Path=PlacementTarget.SelectedItem}"></MenuItem>
                    <Separator/> 
                </ContextMenu>
            </DataGrid.ContextMenu>

我这一行有错误

Private _SuggestSollution As RelayCommand
Public Sub New()
    _TestCollection.Add(New Test(3, 23, 26, "delete * from database", 92, 0, 0, " ", Nothing))

    _SuggestSollution = New RelayCommand(AddressOf DisplaySomemessage, AddressOf ObjectCanUseOpen)
End Sub

'Methods

Public Sub DisplaySomemessage(ByVal message As Object)
    'Some action
End Sub

Public Function ObjectCanUseOpen(ByVal obj As Object) As Boolean
    If (TryCast(obj, Test).ErrorInTest Is Nothing) Then
        Return False
    End If
    Return True
End Function

System.NullReferenceException表示TryCast不返回任何内容

有什么建议吗?谢谢

0 个答案:

没有答案