C# 双击WPF数据网格中的单个单元格时,如何更改其值?
我有一个datagrid,它列出了一些事情的状态,并显示“True”或“False”。当我双击一个单元格时,我想切换该单元格显示的内容。我已经尝试为我的datagrid使用了许多属性,例如currentItem、SelectedItem、SelectedValue和SelectedUnit,但这些都不起作用C# 双击WPF数据网格中的单个单元格时,如何更改其值?,c#,wpf,datagrid,C#,Wpf,Datagrid,我有一个datagrid,它列出了一些事情的状态,并显示“True”或“False”。当我双击一个单元格时,我想切换该单元格显示的内容。我已经尝试为我的datagrid使用了许多属性,例如currentItem、SelectedItem、SelectedValue和SelectedUnit,但这些都不起作用 <DataGrid x:Name="dataGrid1" IsReadOnly="True" IsManipulationEnabled="False" Width="250" Hei
<DataGrid x:Name="dataGrid1" IsReadOnly="True" IsManipulationEnabled="False" Width="250" Height="468" Margin="795,15,18,15"
VerticalAlignment="Top" AutoGenerateColumns="False" CanUserAddRows="False" MouseDoubleClick="DoubleClick"
ItemsSource="{Binding Availability}">
<DataGrid.Columns>
<DataGridTextColumn Header="Number" Binding="{Binding Key, UpdateSourceTrigger=PropertyChanged}" Width="Auto"/>
<DataGridTextColumn Header="Status1" Binding="{Binding Value1, UpdateSourceTrigger=PropertyChanged}" Width="Auto"/>
<DataGridTextColumn Header="Status2" Binding="{Binding Value2, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
如果您想知道双击了哪个DataGridCell,必须将其添加到XAML中:
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<EventSetter Event="MouseDoubleClick" Handler="dataGrid1_CellDoubleClick" />
</Style>
</DataGrid.CellStyle>
问题是。。。您的DataGrid和列是数据绑定的,因此您对单元格内容所做的大多数操作不会反映在实际数据中
我说“most”,因为实际上,您可以同时更改单元格文本和数据绑定属性的值。。。但是它很脏,不应该出现在视野中
private void dataGrid1_CellDoubleClick(object sender, RoutedEventArgs e)
{
var cell = sender as DataGridCell;
if (cell != null && cell.Content is TextBlock)
{
var textBlock = cell.Content as TextBlock;
textBlock.SetCurrentValue(TextBlock.TextProperty, "put your text here");
var binding = BindingOperations.GetBindingExpression(textBlock, TextBlock.TextProperty);
binding.UpdateSource();
}
}
要使其工作,您必须在DataGridTextColumn绑定上设置Mode=TwoWay
(尽管它可能已经是默认模式,我不记得了)。此外,它也不能用于其他类型的列
但是。。。正如我所说,这个解决方案是肮脏的,您希望在viewmodel中具有这种逻辑
最简单的方法是从viewmodel中公开一个方法,您可以通过代码调用该方法,并传递一些参数,如行的数据项和列的属性名
private void dataGrid1_CellDoubleClick(object sender, RoutedEventArgs e)
{
var cell = sender as DataGridCell;
if (cell != null)
{
(this.DataContext as MyViewModel).DoStuff(cell.DataContext, cell.Column.SortMemberPath);
}
}
您的DataGrid已绑定到数据源,因此您需要更新该数据源,然后重新加载数据。我知道如何使用dataGrid1.SelectedIndex确定单击了哪一行,但如何确定单击了哪一列?例如,如果双击中间列中的任意行,则与Value1关联。我如何告诉我的数据源它需要切换Value1而不是Value2?我似乎需要类似dataGrid1.SelectedColumn的东西,但我在msdn的网站上没有看到该属性。
private void dataGrid1_CellDoubleClick(object sender, RoutedEventArgs e)
{
var cell = sender as DataGridCell;
if (cell != null && cell.Content is TextBlock)
{
var textBlock = cell.Content as TextBlock;
textBlock.SetCurrentValue(TextBlock.TextProperty, "put your text here");
var binding = BindingOperations.GetBindingExpression(textBlock, TextBlock.TextProperty);
binding.UpdateSource();
}
}
private void dataGrid1_CellDoubleClick(object sender, RoutedEventArgs e)
{
var cell = sender as DataGridCell;
if (cell != null)
{
(this.DataContext as MyViewModel).DoStuff(cell.DataContext, cell.Column.SortMemberPath);
}
}