[英]How do I change the value of an individual cell in a WPF datagrid when it is double clicked?
I have a datagrid that lists the status of a couple things and displays either "True" or "False". 我有一个数据网格,其中列出了几件事情的状态并显示“ True”或“ False”。 When I double click on a cell, I want to toggle what that cell displays.
当我双击一个单元格时,我想切换该单元格显示的内容。 I've tried using a number of properties for my datagrid, such as currentItem, SelectedItem, SelectedValue, and SelectedUnit, but none of these have worked.
我已经尝试为我的数据网格使用许多属性,例如currentItem,SelectedItem,SelectedValue和SelectedUnit,但是这些都不起作用。
<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>
Here's the event in my codebehind: 这是我背后的代码中的事件:
private void DoubleClick(object sender, MouseButtonEventArgs e)
{
if (dataGrid1.SelectedItem == null) return;
if (dataGrid1.CurrentItem == "True" )
{
dataGrid1.CurrentItem = "False";
}
else if (dataGrid1.CurrentItem == "False")
{
dataGrid1.CurrentItem = "True";
}
}
If you wanna know which DataGridCell is being double-clicked, you must add this to your XAML: 如果您想知道双击哪个DataGridCell,则必须将其添加到XAML中:
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<EventSetter Event="MouseDoubleClick" Handler="dataGrid1_CellDoubleClick" />
</Style>
</DataGrid.CellStyle>
And then in your code-behind, handle the event: 然后在您的代码隐藏中,处理事件:
private void dataGrid1_CellDoubleClick(object sender, RoutedEventArgs e)
{
var cell = sender as DataGridCell;
// Do stuff with your cell
}
The problem is... Your DataGrid and columns are databound, so most things you do to your cell's content won't reflect in your actual data. 问题是...您的DataGrid和列是数据绑定的,因此您对单元格内容所做的大多数操作都不会反映在实际数据中。
I say " most ", because you could, in fact, change the cell text and the value of the databound property at the same time... But it's quite dirty and doesn't really belong in the view. 我说“ 最 ”,是因为实际上您可以同时更改单元格文本和数据绑定属性的值...但是它很脏,实际上并不属于视图。
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();
}
}
For this to work, you have to set Mode=TwoWay
on your DataGridTextColumn Bindings (though it may already be the default mode, I don't remember). 为此,您必须在DataGridTextColumn绑定上设置
Mode=TwoWay
(尽管它可能已经是默认模式,但我不记得了)。 Also, it won`t work with other types of columns. 而且,它不能与其他类型的列一起使用。
BUT... As I said, this solution is dirty and you want to have that kind of logic in your viewmodel. 但是...正如我所说,此解决方案很脏,您希望在视图模型中使用这种逻辑。
The easiest way to do that could be exposing a method from your viewmodel, that you could call from code-behind passing some parameters like the row's data item and the column's property name. 最简单的方法是从视图模型中公开一个方法,您可以在代码中调用该方法,然后传递一些参数,例如行的数据项和列的属性名。
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.