我有這個標記如何僅在DataGrid中選擇一行時啓用按鈕?
<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
}
我CanExecute
我RelayCommand
返回true方法如果在DataGrid中選擇一個項目。
但是,窗口打開時未選擇任何項目,導致按鈕被禁用。如果我在DataGrid上選擇一些東西,則不會發生任何事
如何在DataGrid
中選擇一行時「刷新」按鈕?
您正在使用哪個'RelayCommand'版本? –