c# - WPF DataGrid : Displaying 2 decimal places when not editing, 但编辑时为完整数字

标签 c# wpf datagrid wpfdatagrid

我正在尝试创建一个数据网格,当用户输入值并按下回车键时,它会以 2 位小数精度显示数据。

但是当他们点击编辑它时,我希望他们能够再次查看整个数字。

我目前拥有的是:

<DataGridTextColumn Header="s" Binding="{Binding s, StringFormat=N2}" ElementStyle="{StaticResource TextColumnWhite}" >
    <DataGridTextColumn.EditingElementStyle>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Text" Value="{Binding s}" />
            <Setter Property="Background" Value="Red" />
        </Style>
    </DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>

数据网格单元格显示红色背景,但里面的数字没有更新为非格式化值。

感谢您的帮助

最佳答案

您需要在父级别移除字符串格式(覆盖 EditingElementStyle)——而是仅在 EditingElementStyle 的样式中为绑定(bind)表达式设置字符串格式——但也为常规 ElementStyle(非编辑mode) 这是一个 TextBlock 样式:

<DataGridTextColumn Header="s" ElementStyle="{StaticResource TextColumnWhite}">

    <!-- editing view -->
    <DataGridTextColumn.EditingElementStyle>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Text" Value="{Binding s}" />                            
            <Setter Property="Background" Value="Red" />
        </Style>                        
    </DataGridTextColumn.EditingElementStyle>

    <!-- not editing view -->
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Text" Value="{Binding s, StringFormat=N2}" />
            <Setter Property="Background" Value="Transparent" />
        </Style>
    </DataGridTextColumn.ElementStyle>

</DataGridTextColumn>

关于c# - WPF DataGrid : Displaying 2 decimal places when not editing, 但编辑时为完整数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30579362/

相关文章:

c# - WPF : Set ListViewItem from textblock in the Listview within a DataTemplate

c# - 如何检测和管理由加载的 DLL 执行的磁盘/注册表/网络事件?

c# - 当绑定(bind)属性为空时,WPF DataGrid 显示 'NULL'

c# - 动态数据显示 : Change X-Axis date time format for graph

.net - Silverlight 本地化 - 如何覆盖 Windows 区域性

c# - WPF DataGrid 单元格值更改事件

asp.net - css 在表 asp.net datagrid 中指定第一个 tr

c# - 实现 DataAnnotations 验证属性时,我应该调用 base.IsValid() 吗?

c# - "Cannot resolve scoped service from root provider"带有自定义 EF Core SeriLog Sink

c# - 使用 WPF 绘制数千个数据点的最高效方法?