我有这个标记
<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}"/>
我的RelayCommand
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
方法RelayCommand
返回true。
但是窗口打开时未选择任何项目,从而导致按钮被禁用。如果我在DataGrid上选择某些内容,则不会发生任何事情。
如果已在中选择了行,如何“刷新”按钮DataGrid
?
我将活动更改为
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
现在它正在工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句