我有这个标记
<DataGrid Margin="10,10,10,48" AutoGenerateColumns="False" Name="grdUsers"
ItemsSource="{Binding Users}"
>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="*" Binding="{Binding Name}" />
<DataGridTextColumn Header="Username" Width="*" Binding="{Binding Username}" />
<DataGridTextColumn Header="Password" Width="*" Binding="{Binding Password}" />
<DataGridTextColumn Header="Role" Width="*" Binding="{Binding Path=Role.Name}" />
</DataGrid.Columns>
</DataGrid>
<Button Content="Add" HorizontalAlignment="Left" Margin="10,0,0,10" Width="75" Height="20" VerticalAlignment="Bottom"/>
<Button Content="Edit" HorizontalAlignment="Left" Margin="90,0,0,10" Width="75" Height="20" VerticalAlignment="Bottom"/>
<Button Content="Remove" Margin="0,0,10,10" Height="20" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="75"
Command="{Binding Remove}" CommandParameter="{Binding ElementName=grdUsers, Path=SelectedItem}"/>
我的中继命令
public class RelayCommand : ICommand
{
private readonly Action<object> execute = null;
private readonly Predicate<object> canExecute = null;
public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
{
this.execute = execute;
this.canExecute = canExecute;
}
#region ICommand members
public bool CanExecute(object parameter)
{
return canExecute == null ? true : canExecute(parameter);
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
execute(parameter);
}
#endregion
}
如果在 DataGrid 中选择了一个项目,我的CanExecute
方法返回 true。RelayCommand
但是窗口打开时没有选择任何项目,导致按钮被禁用。如果我在 DataGrid 上选择某些内容,则不会发生任何事情。
如果在 中选择了一行,我如何“刷新”按钮DataGrid
?