datagrid上的BindingExpression路径错误

时间:2015-05-04 08:16:00

标签: c# wpf datagrid

我有一个表示五列的数据网格,最后一列只包含按钮。

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')

我做错了什么?

2 个答案:

答案 0 :(得分:1)

You Button的DataContext是它所在的DataGridRow的DataItem。

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

答案 1 :(得分:1)

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

因此,您必须将Button的DataContext设置为DataGrid的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>