简体   繁体   English

如何使用一个绑定多个文本框来分隔 WPF XAML 中的三位数字?

[英]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.

相关问题 如何找到每个数字都在3中且所有数字相加等于数字本身的所有三位数 - How can I find all the three-digit numbers that each digit is in 3 and the addition of the digits is equal to the number itself 将Windows窗体(C#)中的三位数字分开并恢复原状 - Separate the three-digit numbers in Windows form(C # ) and return them to the original state 如何在WPF中将多个组合框与一个数据源一起使用? - How can I use multiple combo boxes with one datasource in WPF? 如何在一个 xaml 文件上使用单独的 C# 文件 - How do I use a separate C# file on one xaml file 如何在WPF应用程序中从多个窗口引用文本框? - How do I reference text boxes from multiple windows in a WPF application? 如何在另一个项目中使用自定义WPF / XAML控件? - How do I use a custom WPF/XAML control in another project? 枚举器Int值转换为以三位数为基数的字符串2 - Enumerator Int value convert to string three-digit base 2 number 如何在一个xaml页面中使用多个用户控件? - How do I use multiple user-controls inside one xaml page? 如何在DataGridTextColumn WPF XAML中将绑定值设置为TargetNullValue属性 - How do i set a Binding Value to a TargetNullValue property in a DataGridTextColumn WPF XAML 如何在wpf中的文本绑定前添加项目符号? - How do I add a bullet point in front of a text binding in wpf?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM