简体   繁体   English

双击WPF数据网格中的单个单元格,如何更改它的值?

[英]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.

相关问题 在C#WPF中的DataGrid中双击特定单元格时,如何弹出消息框或文本框 - How can I make a messagebox or textbox pop up when a specific cell is double clicked in my DataGrid in C# WPF 单击DataGrid单元格中的按钮时更改其图像-WPF - Change image of a button in a datagrid cell when it is clicked - wpf 单击行时可以更改单元格样式WPF DataGrid吗? - Possible to change cell style when row is clicked WPF DataGrid? 如何找到在WPF上的DataGrid中单击的单元格的行值? - How to find the row value of the cell clicked in a DataGrid on WPF? 当我双击WPF DataGrid行以获取单元格值时,返回NULL - When i double click on WPF DataGrid row for get cell value, return NULL 如何将所选 WPF DataGrid 单元格的值设置为上面单元格的值? - How do I set the value of the selected WPF DataGrid cell to the value of the cell above? 在wpf DataGrid中单击DataGridTemplateColumn中的按钮时如何停止行选择 - How do I stop row selection when a button in DataGridTemplateColumn is clicked in wpf DataGrid 如何捕获WPF中Datagrid单元的值更改事件? - How to capture value change event of Datagrid's cell in WPF? 如何根据值更改WPF数据网格中单元格的背景颜色 - How to change background color of a cell in WPF datagrid based on its value 如何根据itemsource中对象的属性更改WPF DataGrid中单元格的颜色? - How do I change the color of Cell in WPF DataGrid based on a property of the object in itemsource?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM