【问题标题】:DataGrid cells - convert decimal and binary numbers to hexaDataGrid 单元格 - 将十进制和二进制数字转换为十六进制
【发布时间】:2014-06-12 08:03:17
【问题描述】:

在我的 WPF 应用程序中,我有一个填充 DataGrid 的 DataTable,并且可以编辑 DataGrid 中的单元格。 我希望,在特定列中,如果用户输入十进制或二进制数,它将自动更改为十六进制数。 有什么想法吗?

【问题讨论】:

  • 尝试使用 CellEditEnding 事件或实现 notifyPropertychanged
  • 如果您使用 WPF 使用 MvvM 模式,至于 DataGrid 我没有使用它,但我发现了这个wpfTutorial.net。 HTH
  • 是的,但是我如何引用特定的单元格?像 this.convertToHexa 之类的 @moez
  • DataGrid 支持用户对其单元格的编辑。当用户完成编辑时,将触发 CellEditEnding 事件。在这个 C# 方法中,我们可以取消编辑。如果我们检测到无效条目,这很有用。如果您需要,我可以给您一个示例如何获取数据并使其生效

标签: c# wpf wpf-controls wpfdatagrid


【解决方案1】:

有很多方法可以实现您的指定要求。 cmets中描述的方法是处理DataGrid.CellEditEnding event

<DataGrid ItemsSource="{Binding Items}" CellEditEnding="DataGrid_CellEditEnding" />

从链接页面,这个事件:

在提交或取消单元格编辑之前发生。

因此,当用户在任何单元格中输入完一个值后,将引发此事件。因此,您现在需要做的就是从这个事件处理程序中调用一个转换方法:

private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    ((TextBox)e.EditingElement).Text = 
        ConvertToHexadecimal(((TextBox)e.EditingElement).Text);
}

private string ConvertToHexadecimal(string input)
{
    int number = 0;
    bool isInputInteger = int.TryParse(input, out number); 
    return isInputInteger ? number.ToString("X") : input; 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-26
    • 2013-11-24
    • 2014-10-30
    • 1970-01-01
    • 2012-04-08
    • 2016-08-28
    相关资源
    最近更新 更多