我想创建一个至少包含一个整数(例如123456789)的文本框,但它显示了千位分隔符(例如123.456.789),但是当我选择文本框进行编辑时,字符串返回时没有直到文本框失去焦点,就像在Excel中一样。有什么建议?谢谢!excel-like wpf textbox
1
A
回答
2
使用触发器如果文本框没有选择
<Style TargetType="{x:Type TextBox}">
<Setter Property="Text" Value="{Binding SomeValue, StringFormat=N2}" />
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="Text" Value="{Binding SomeValue}" />
</Trigger>
</Style.Triggers>
</Style>
你也可以使用一个转换器,用于格式化如果你不能很容易地的StringFormat格式格式化值
0
一种可能性:
您添加了在IsFocused上创建触发器的样式。 在触发器中设置了一个新的模板,其中您有另一种格式:
<Grid>
<Grid.Resources>
<System:Double x:Key="boundDouble">1000</System:Double>
<System:Double x:Key="boundDouble2">2000</System:Double>
</Grid.Resources>
<TextBox Width="100" Height="30">
<TextBox.Text>
<Binding Source="{StaticResource boundDouble}" Path="." StringFormat="{}{0:F3}" />
</TextBox.Text>
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsFocused" Value="true">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<TextBox>
<TextBox.Text>
<Binding Source="{StaticResource boundDouble}" Path="." StringFormat="{}{0:F5}" />
</TextBox.Text>
</TextBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</Grid>
相关问题
- 1. WPF TextBox Stretching
- 2. wpf textbox textwrapping
- 3. TextBox Inside ListView WPF
- 4. WPF Label to TextBox
- 5. textbox wpf到右
- 6. WPF Listview Textbox Highlight
- 7. wpf textbox databinding
- 8. WPF TextBox截断
- 9. WPF TextBox RaiseEvent
- 10. wpf textbox caret
- 11. 在WPF的TextBox
- 12. WPF TextBox拦截RoutedUICommands
- 13. WPF TextBox ValidationRule绑定
- 14. WPF TextBox MaxLength警告
- 15. WPF Textbox上的TextPreview
- 16. WPF Textbox TAB空间
- 17. WPF中的TextBox到PasswordBox
- 18. WPF中的TextBox选项
- 19. WPF TextBox副本被截断
- 20. C#:Tooltip on Focus on WPF TextBox
- 21. WPF TextBox no允许空间
- 22. WPF MVVM textBox文本绑定
- 23. Textbox Enter Key event not in WPF
- 24. WPF TextBox插入消失
- 25. WPF默认TextBox ContextMenu样式
- 26. WPf中支持Intellisense的TextBox
- 27. WPF TextBox自动增长
- 28. WPF TextBox将不会填写StackPanel
- 29. C#WPF功能区:RibbonTextBox与TextBox
- 30. WPF TextBox lostfocus作为附加属性
谢谢,完美的作品!只有一件事,你知道我如何将千位分隔符从“,”改为“”。直接在xaml setter表达式中? StackOverFlow社区,你真棒:) – 2011-12-30 15:30:55
@GiacomoTagliabue你可能不得不使用'Converter'来代替'StringFormat'。只需格式化为正常格式即可。'替换()'带点的逗号 – Rachel 2011-12-30 15:34:02
谢谢,我不想使用'Converter',但在这种情况下它似乎是唯一的可能性 – 2011-12-30 15:37:06