[英]How do I use one binding for multiple text boxes to separate three-digit numbers in WPF XAML?
I use C# WPF in Visual Studio 2019我在 Visual Studio 2019 中使用 C# WPF
I want to display my TextBoxes values as Thousands decimal separated like this 1500.9 => 1,500.9
我想将我的 TextBoxes 值显示为像这样
1500.9 => 1,500.9
分隔的千位小数
I Choose Validation Rules and Binding for this problem because I don't want to use C# Code Behind and Another point is that this option alone does nothing for a text box我为这个问题选择了验证规则和绑定,因为我不想使用 C# 代码背后和另一点是这个选项本身对文本框没有任何作用
StringFormat="0,0.0"
What is My Problem :我的问题是什么:
I thought Binding in Validation Rule uses this type of property for formatting, I did bind all of my textboxes to my property and now all text boxes have the same value even when I change one of them enter image description here我认为验证规则中的绑定使用这种类型的属性进行格式化,我确实将所有文本框都绑定到我的属性,现在所有文本框都具有相同的值,即使我更改其中一个在此处输入图像描述
My Project :我的项目:
here is my Model name as Validator.cs
:这是我的 Model 名称为
Validator.cs
:
namespace WpfApp5
{
public class Validator
{
public double dbl_numeric { get; set; }
}
}
My window for binding textboxes:我的 window 用于绑定文本框:
<Window x:Class="WpfApp5.Budget_Default"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:gif="http://wpfanimatedgif.codeplex.com"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp5"
mc:Ignorable="d"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:validate="clr-namespace:WpfApp5.Validation"
AllowDrop="True"
Height="550" Width="1033" >
<Grid x:Name="mygrid">
<TextBox x:Name="TextBox1" >
<Binding Path = "dbl_numeric" UpdateSourceTrigger = "PropertyChanged" StringFormat="0,0.0">
<Binding.ValidationRules>
<ExceptionValidationRule ValidatesOnTargetUpdated = "True"/>
<validate:RequeredViladation/>
</Binding.ValidationRules>
</Binding>
</TextBox>
<TextBox x:Name="TextBox2" >
<Binding Path = "dbl_numeric" UpdateSourceTrigger = "PropertyChanged" StringFormat="0,0.0">
<Binding.ValidationRules>
<ExceptionValidationRule ValidatesOnTargetUpdated = "True"/>
<validate:RequeredViladation/>
</Binding.ValidationRules>
</Binding>
</TextBox>
<TextBox x:Name="TextBox3" >
<Binding Path = "dbl_numeric" UpdateSourceTrigger = "PropertyChanged" StringFormat="0,0.0">
<Binding.ValidationRules>
<ExceptionValidationRule ValidatesOnTargetUpdated = "True"/>
<validate:RequeredViladation/>
</Binding.ValidationRules>
</Binding>
</TextBox>
</Grid>
What I need :我需要什么:
How to use Validation Rule or something to Display Thousands of Separated decimal numbers to use strings format for my textboxes如何使用验证规则或其他东西来显示数千个分隔的十进制数字以对我的文本框使用字符串格式
long story short: Each textbox should be separated into 3 digits, 3 digits for its own value only长话短说:每个文本框应该分成 3 位数字,3 位数字只代表它自己的值
1500.9 => 1,500.9 1500.9 => 1,500.9
If all you want is to format the value bound to the textbox, this would suffice:如果您只想格式化绑定到文本框的值,这就足够了:
<TextBox Text="{Binding Path=dbl_numeric,StringFormat={}{0:N2}}" />
This will give you a culture specific separator for thousands, and two decimal places.这将为您提供千位和两位小数的特定文化分隔符。
Validation is another topic.验证是另一个主题。 Note that out of the box, WPF will check if a value can be parsed as a double, and put a red border around a textbox if, for example, you input text.
请注意,开箱即用,WPF 将检查一个值是否可以解析为双精度值,并在文本框周围放置一个红色边框,例如,如果您输入文本。 You may want to set
UpdateSourceTrigger
to LostFocus
if you want to allow a user to type in a complete value before validating.如果您希望允许用户在验证之前输入完整的值,您可能需要将
UpdateSourceTrigger
设置为LostFocus
。 If you have other validation needs, you will have to manually add this.如果您有其他验证需求,则必须手动添加。
You three text boxes are currently all bound to the same value in the model (which typically would be a view model in WPF).您的三个文本框当前都绑定到 model 中的相同值(通常是 WPF 中的视图 model)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.