2015-03-13 71 views
0

所以我找到了這個答案Pass command parameter from the xaml,我認爲這對我有很大的幫助。我遇到的問題是,當我在數據網格中選擇一行時,它會觸發命令,但所選項目爲空。命令參數Datagrid SelectedItems爲空

我不知道,懷疑是問題,應該將選定項目傳遞給視圖模型的類型是什麼?目前我使用的IList如在我的視圖模型代碼:

namespace Project_Manager.ViewModel 
{ 
public class ProjectSummaryViewModel : ObservableObject 
{ 
    public ProjectSummaryViewModel() 
    { 
     ProjectSummary = DatabaseFunctions.getProjectSummaryData(); 
    } 

    private ObservableCollection<ProjectSummaryModel> projectsummary; 
    public ObservableCollection<ProjectSummaryModel> ProjectSummary 
    { 
     get { return projectsummary; } 
     set 
     { 
      projectsummary = value; 
      OnPropertyChanged("ProjectSummary"); 
     } 
    } 

    public ICommand DeleteRowCommand 
    { 
     get { return new ParamDelegateCommand<IList<ProjectSummaryModel>>(DeleteRow); } 
    } 

    private void DeleteRow(IList<ProjectSummaryModel> projectsummaryselected) 
    { 
     string name = projectsummaryselected[0].ProjectName; 
    } 
} 
} 

DataGrid的XAML視圖代碼看起來是這樣的:

<Window x:Class="Project_Manager.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 

<!--<Window.Resources> 
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /> 
</Window.Resources>--> 

    <Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> <!--Menu row--> 
     <RowDefinition Height="Auto"/> <!--button row--> 
     <RowDefinition/> <!--Current projects--> 
     <RowDefinition/> <!--Completed projects--> 
    </Grid.RowDefinitions> 

    <!--<Menu> 
     <MenuItem Header="File"> 
      <MenuItem Header="New Project Management" CommandTarget="{Binding NewTable}"/> 
      <MenuItem Header="Open Project Management"/> 
      <Separator/> 
      <MenuItem Header="Exit"/> 
     </MenuItem> 
     <MenuItem Header="View"> 
      <MenuItem x:Name="ViewCompleted" IsCheckable="True" IsChecked="True" Header="View Completed Projects List"/> 
     </MenuItem> 
     <MenuItem Header="Project Management"> 
      <MenuItem Header="New Project"/> 
     </MenuItem> 

    </Menu>--> 

    <StackPanel Grid.Row="1" Orientation="Horizontal"> 
     <Button Content="Create New Project" Command="{Binding Path=NewProjectCommand}"/> 
     <!--<Button Content="View Details" Visibility="{Binding Source={x:Reference Name=CurrentProjectsDataGrid}, Path=IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}"/>--> 
    </StackPanel> 

    <Grid Grid.Row="2"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 
     <TextBlock Text="Current Projects" Background="LemonChiffon"/> 
     <DataGrid x:Name="SummaryDataGrid" ItemsSource="{Binding ProjectSummary}" Grid.Row="1" AutoGenerateColumns="True" Style="{StaticResource DataGridStyle}"> 
      <DataGrid.ContextMenu> 
       <ContextMenu> 
        <MenuItem Header="Delete Row" Command="{Binding DeleteRowCommand}" CommandParameter="{Binding SelectedItems, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"/> 
       </ContextMenu> 
      </DataGrid.ContextMenu> 
     </DataGrid> 
    </Grid> 


    <!--<DataGrid.Columns> 
       <DataGridTextColumn Header="Project Name" Binding=""/> 
       <DataGridTextColumn Header="Team Name"/> 
       <DataGridTextColumn Header="Latest Update"/> 
       <DataGridTextColumn Header="Date Started"/> 
      </DataGrid.Columns>--> 

    <!--<Grid Grid.Row="3"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 
     --><!--<TextBlock Text="Completed Projects" Background="LightGreen" Visibility="{Binding Source={x:Reference Name=ViewCompleted}, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}"/> 
     <DataGrid Grid.Row="1" AutoGenerateColumns="False" Style="{StaticResource DataGridStyle}" Visibility="{Binding Source={x:Reference Name=ViewCompleted}, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}"> 
      <DataGrid.Columns> 
       <DataGridTextColumn Header="Project Name"/> 
       <DataGridTextColumn Header="Team"/> 
       <DataGridTextColumn Header="Date Completed"/> 
      </DataGrid.Columns> 
     </DataGrid>--><!-- 
    </Grid>--> 
</Grid> 

和公正的情況下,這是一個命令執行這裏的問題是我正在使用的自定義委託命令:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Input; 

namespace Project_Manager.Common 
{ 
public class ParamDelegateCommand<T> : ICommand 
{ 
    public event EventHandler CanExecuteChanged; 

    private Action<T> executeMethod; 

    //private Func<T, bool> canExecuteMethod; 

    public ParamDelegateCommand(Action<T> executeMethod) //, Func<T, bool> canExecuteMethod) 
    { 
     this.executeMethod = executeMethod; 
     //this.canExecuteMethod = canExecuteMethod; 
    } 

    public bool CanExecute(object parameter) 
    { 
     return true; //canExecuteMethod((T)parameter); 
    } 

    public void Execute(object parameter) 
    { 
     executeMethod((T)parameter); 
    } 
} 
} 

I已經搜索過,可以找到很多XAML綁定的例子,我似乎無法找到它的另一半。那麼在視圖模型中應該是什麼類型?或者,實際問題是什麼?

編輯:剛纔注意到一個錯誤調試窗口,可以幫助如果您使用的上下文菜單,請參考下面的代碼來獲得SelectedItems有人

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=SelectedItems; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'CommandParameter' (type 'Object')

回答

3

<ContextMenu> 
        <MenuItem Header="Delete Row" Command="{Binding DeleteRowCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}"/> 
       </ContextMenu> 

另外Selecteditems是一個IList所以更改下面的代碼。

public ICommand DeleteRowCommand 
{ 
    get { return new ParamDelegateCommand<IList>(DeleteRow); } 
} 

private void DeleteRow(IList projectsummaryselected) 
{ 
    string name = (projectsummaryselected[0] as ProjectSummaryModel).ProjectName; 
} 
+0

問題!我現在得到selecteditem,但如果我使用'string name = projectsummaryselected [0] .ProjectName;'我得到一個錯誤**對象不包含ProjectName的定義,並且找不到擴展方法**。這很奇怪,因爲如果我看'projectsummaryselected [0] .ToString();'然後我看到具有值的ProjectName。我如何得到它? – chris 2015-03-13 18:40:30

+0

編輯我的答案。你需要進行類型轉換來獲取列表。 – 2015-03-13 19:26:18

+0

如果其他人發現這一點,並獲得相同的點;在這裏的答案的幫助:[如何獲得單一的值列表](http://stackoverflow.com/questions/19206676/how-to-get-single-value-of-listobject)我能夠圖出來。我結束了使用'foreach(ProjectSummaryModel項目在projectsummaryselected){item.ProjectName}給我的價值。謝謝大家的幫助! – chris 2015-03-13 19:30:27

1

試試這個

<MenuItem Header="Delete Row" Command="{Binding DeleteRowCommand}" CommandParameter="{Binding SelectedItems, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"/> 
+0

Thank you @Muds for the response but still get null。我用我在調試窗口中注意到的錯誤消息編輯了我的帖子。不知道這是否有幫助。 – chris 2015-03-13 17:19:01

+0

我稍微更改了代碼,這對我的作品 – Muds 2015-03-13 17:22:30

+0

,如果它仍然不會工作,發佈一些更多的代碼來了解 – Muds 2015-03-13 17:22:50