【问题标题】:How to allow only Positive decimal value in WPF DataGridTextColumn如何在 WPF DataGridTextColumn 中只允许正十进制值
【发布时间】:2012-10-16 07:18:20
【问题描述】:

我想在 DataGridTextColumn 中只允许正十进制值,如果用户输入 .369,系统将显示 0.369 怎么可能。我是 WPF 的新手

【问题讨论】:

标签: wpf


【解决方案1】:

试试这个

XAML:

   <Window x:Class="SandBox.Window4"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:SandBox"
            Title="Window4" Height="300" Width="300">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <DataGrid x:Name="dg" Grid.Row="0" >
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="Quantity" Width="100">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Path=Qty, Mode=TwoWay}" x:Name="tb" TextChanged="tb_TextChanged">

                                </TextBox>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Window>

这是代码隐藏:

   using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.Collections.ObjectModel;

    namespace SandBox
    {
        /// <summary>
        /// Interaction logic for Window4.xaml
        /// </summary>
        public partial class Window4 : Window
        {
            public ObservableCollection<ItemClass> Collection = new ObservableCollection<ItemClass>();
            public Window4()
            {
                InitializeComponent();
                //Collection.Add(new ItemClass("123"));
                //Collection.Add(new ItemClass("456"));
                Collection.Add(new ItemClass("123"));
                Collection.Add(new ItemClass("456"));
                dg.ItemsSource = Collection;
            }

            private void tb_TextChanged(object sender, TextChangedEventArgs e)
            {
                int result = 0;
                TextBox txt = sender as TextBox;
                if (txt != null)
                {
                    if (!int.TryParse(txt.Text, out result))
                    {
                        if(result >= 0)
                        {
                            txt.Text = txt.Text.Substring(0, txt.Text.Length - 1);
                            txt.CaretIndex = txt.Text.Length;
                        }
                    }
                }
            }
        }

        public class ItemClass
        {
            public string Qty { get; set; }
            public ItemClass(string qty)
            {
                Qty = qty;
            }
        }
    }

【讨论】:

    【解决方案2】:

    为此,您可能必须在 TextColumn 的 Binding 中设置适当的 StringFormat

    试试这个...

       <Controls:DataGridTextColumn Header="Quantity"
                                    Binding="{Binding Quantity, 
                                             StringFormat='{}{0:0.####}'}"/>
    

    所以现在如果用户输入 .897 并按下回车键,单元格值将显示为“0.897”。

    【讨论】:

    • 它不工作有一些焦点问题当我输入 .36 它写 .63 你能告诉我解决方案
    • 你有像 UrduArabic 语言那样从右到左的 TextAlignmentFlowDirection 吗?
    • Nadeem,这对我有用。我很确定您的语言设置导致文本错位。请将您的区域和语言(控制面板 -> 区域和语言选项)更改为美国或英国,然后重新启动您的应用程序。不要忘记在区域选项卡和语言(单击详细信息按钮)选项卡上设置两种设置
    • 设置如你所说但不知道为什么
    猜你喜欢
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 2019-04-22
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多