我想禁用(关闭编辑)我的dataGrid的第3列(使用MVVM-WPF),但是想要在Grid外点击按钮再次编辑。我能实现吗? 我在这里面临的挑战是如何在按钮点击时检索dataGrid的属性,我在这里使用命令模式。这就是我的ViewModel现在的样子,我应该在那些方法中编写什么来使某些列可编辑:
public class TicketOverViewViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private TicketDataService ticketDataService;
private ObservableCollection<Ticket> tickets;
public ObservableCollection<Ticket> Tickets
{
get
{
return tickets;
}
set
{
tickets = value;
RaisePropertyChanged("Tickets");
}
}
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public TicketOverViewViewModel()
{
ticketDataService = new TicketDataService();
LoadData();
LoadCommands();
}
private void LoadData()
{
tickets = ticketDataService.GetAllTickets().ToObservableCollection();
}
public ICommand EditCommand { get; set; }
private void LoadCommands()
{
EditCommand = new CustomCommand(EditTicket, CanEditTicket);
}
private void EditTicket(object obj)
{
//TODO
}
private bool CanEditTicket(object obj)
{
return true;
}
}
这就是我的Customcommand的外观:
public class CustomCommand : ICommand
{
private Action<object> execute;
private Predicate<object> canExecute;
public CustomCommand(Action<object> execute, Predicate<object> canExecute)
{
this.execute = execute;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
bool b = canExecute == null ? true : canExecute(parameter);
return b;
}
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
}
remove
{
CommandManager.RequerySuggested -= value;
}
}
public void Execute(object parameter)
{
execute(parameter);
}
}
我的观点:
<Window x:Class="DataGridMVVM.View.TicketOverView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataGridMVVM.View"
mc:Ignorable="d"
Title="Ticket Overview" Height="348.936" Width="421.277"
DataContext= "{Binding Source={StaticResource mainViewModelLocator}, Path=TicketOverViewViewModel }">
<Grid>
<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" VerticalAlignment="Top"
AutoGenerateColumns="True" ItemsSource="{Binding Tickets}" IsReadOnly="True"
CanUserResizeColumns="True" Height="100" Width="400" />
<Button x:Name="button" Content="Edit" Command="{Binding EditCommand}"
HorizontalAlignment="Left" Margin="239,183,0,0" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>
答案 0 :(得分:0)
Swap around the read only attributes on each cell. So start like this
dataGridView1.Rows[row.index].Cells[column.index].ReadOnly= true;
then on button click
dataGridView1.Rows[row.index].Cells[column.index].ReadOnly= false;
you can set the whole row or column like this
dataGridView1.Rows[row.index].ReadOnly= false;
dataGridView1.Columns[columns.index].ReadOnly= false;
or the entire grid
dataGridView1.ReadOnly = false;