2015-06-01 79 views
1

我试图建立一个数据网格,当用户输入数值并按下回车键时,它会显示精度为2个小数点的数据。WPF DataGrid:在不编辑时显示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> 

红色背景显示为DataGrid细胞内,但数量不会更新到非格式化的值。

感谢所有帮助

+0

您可能需要为该列设置遮罩。 – Versatile

+0

@多才多艺你能解释一下我会怎么做? – bgore

回答

2

你需要带走在父级别的字符串格式(这是压倒一切的EditingElementStyle) - ,而是设置只在样式EditingElementStyle绑定表达式字符串格式 - 但也适用于TextBlock样式的常规ElementStyle(非编辑模式):

<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> 
+0

是的,这是正确的答案,我在回家的路上意识到这一点,感觉像个白痴。谢谢! – bgore