2015-05-04 107 views
0

我有一个datagrid代表五列,最后一列只包含按钮。datagrid BindingExpression路径错误

enter image description here

的DataGrid中的XAML代码如下:

<DataGrid.Columns> 
       <DataGridTextColumn Header="Prefix" Binding="{Binding Prefix, UpdateSourceTrigger=PropertyChanged}" 
            Width="*" /> 
       <DataGridTextColumn Header="Path" Binding="{Binding Path, UpdateSourceTrigger=PropertyChanged}" 
            Width="4*" /> 
       <DataGridTextColumn Header="User" Binding="{Binding User, UpdateSourceTrigger=PropertyChanged}" 
            Width="1*" /> 
       <DataGridTextColumn Header="Password" Binding="{Binding Password, UpdateSourceTrigger=PropertyChanged}" 
            Width="2*" /> 
       <DataGridTemplateColumn Header="Delete"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <Button Command="{Binding DeleteStorage}"> 
           <Button.Content> 
            <Image Source="../Icons/trash.ico" Height="20" Width="20"/> 
           </Button.Content> 
          </Button> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 

      </DataGrid.Columns> 
     </DataGrid> 

我的视图模型:

public class ServerViewModel : ViewModelBase 
    { 
     private readonly ICryptor _cryptorService; 
     private readonly IServerService _serverService; 
     private Storage _editedStorage; 

     public ServerViewModel(IServerService serverService, ICryptor cryptorService) 
     { 
      _serverService = serverService; 
      _cryptorService = cryptorService; 
      _serverService.Query((storages, error) => { Storages = storages; }); 

      AddStorage = new RelayCommand(_addNewStorage); 
      DeleteStorage = new RelayCommand(_deleteRow); 
      NewStorage = new Storage(); 
     } 

     public Storage SelectedStorage { get; set; } 
     public ObservableCollection<Storage> Storages { get; set; } 
     public Storage NewStorage { get; set; } 
     public RelayCommand AddStorage { get; set; } 
     public RelayCommand DeleteStorage { get; set; } 

     private void _addNewStorage() 
     { 
      if (string.IsNullOrEmpty(NewStorage.Prefix) || string.IsNullOrEmpty(NewStorage.Path) || 
       string.IsNullOrEmpty(NewStorage.Password) || string.IsNullOrEmpty(NewStorage.User)) 
      { 
       _sendErrorMsg("Please fill all empty fields."); 
       return; 
      } 

      NewStorage.Prefix = NewStorage.Prefix.ToLower(); 

      if (Storages.Count(e => e.Prefix == NewStorage.Prefix) > 0) 
      { 
       _sendErrorMsg("The prefix is already exists."); 
       return; 
      } 
      NewStorage.Password = _cryptorService.Encrypt(NewStorage.Password, true); 
      Storages.Add(NewStorage); 
      NewStorage = new Storage(); 
     } 

     private void _sendErrorMsg(string text) 
     { 
      if (text == string.Empty) 
      { 
       throw new ArgumentNullException("Pass a message for error messenger."); 
      } 

      GalaMessenger.Default.Send(new StateCommunicator 
      { 
       Type = 'E', 
       Text = text 
      }); 
     } 

     private void _deleteRow() 
     { 
      Debug.WriteLine(SelectedStorage); 
     } 
    } 
} 

当我编译应用程序,我已经得到了错误信息:

System.Windows.Data Error: 40 : BindingExpression path error: 'DeleteStorage' property not found on 'object' ''Storage' (HashCode=49085855)'. BindingExpression:Path=DeleteStorage; DataItem='Storage' (HashCode=49085855); target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand') 
System.Windows.Data Error: 40 : BindingExpression path error: 'DeleteStorage' property not found on 'object' ''Storage' (HashCode=30788008)'. BindingExpression:Path=DeleteStorage; DataItem='Storage' (HashCode=30788008); target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand') 
System.Windows.Data Error: 40 : BindingExpression path error: 'DeleteStorage' property not found on 'object' ''Storage' (HashCode=2382417)'. BindingExpression:Path=DeleteStorage; DataItem='Storage' (HashCode=2382417); target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand') 
System.Windows.Data Error: 40 : BindingExpression path error: 'DeleteStorage' property not found on 'object' ''Storage' (HashCode=13632004)'. BindingExpression:Path=DeleteStorage; DataItem='Storage' (HashCode=13632004); target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand') 

我在做什么错?

回答

1

您巴顿的DataContext的是DataGridRow它在的DataItem的。

<Button Command="{Binding Path=DataContext.DeleteStorage, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}"> 
+0

非常感谢。 –

1

Button的DataContext是DataGrid的DataItem。这是一个存储和存储没有DeleteStorage命令。

所以,你必须为你的按钮将DataGrid的DataContext的设置DataContext的。

这可以通过存档:

<Button Command="{Binding Path=DataContext.DeleteStorage, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}"> 

或:

<DataGrid x:Name="YourDataGrid"> 
    ...  
    <Button Command="{Binding Path=DataContext.DeleteStorage, ElementName=YourDataGrid}"> 
    ... 
</DataGrid>