Datagrid文本框验证,用于MVVM中的数值 [英] Datagrid Textbox validation for numeric values in MVVM

查看:61
本文介绍了Datagrid文本框验证,用于MVVM中的数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我被困在验证DataGrid列中的Textbox.

文本框应仅接受数字值.

XAML代码:

Hi,

I got stuck with validating Textbox which is in DataGrid column.

Textbox should accept only numeric values.

XAML code:

<UserControl x:Class="TestGrid.GridControl"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

             mc:Ignorable="d"

           >

    <StackPanel >
        <DataGrid GridLinesVisibility="Vertical" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"

                                      MinHeight="100" MaxHeight="300" Width="Auto" Name="McDataGrid" Grid.Row="1" Grid.Column="2" AutoGenerateColumns="False" CanUserAddRows="False"

                              CanUserDeleteRows="False" ItemsSource="{Binding Path=TestData}" DataContext="{Binding}"

                  >
            <DataGrid.Columns>
                <DataGridCheckBoxColumn Binding="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False">
                    <DataGridCheckBoxColumn.Header>
                        <CheckBox Name="selectallchkbox3" IsChecked="{Binding DataContext.SelectAll1, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}},
                                            UpdateSourceTrigger=PropertyChanged }"/>

                    </DataGridCheckBoxColumn.Header>
                </DataGridCheckBoxColumn>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Name,Mode=OneWay}" IsReadOnly="True" Width="250"/>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=InstruQuantity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="100" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </StackPanel>
</UserControl>



和模型查看代码:



And Model view code:

namespace ViewModel
{
    public class Authors: ViewModel.ViewProperty
    {
        public Authors()
        {
            LoadCollectionData();
        }

        private DataTable authors = new DataTable();

        public DataTable TestData
        {
            get
            {
                return authors;
            }
        }

        public void LoadCollectionData()
        {
            DataColumn col = new DataColumn();
            col.ColumnName = "Name";
            col.DataType = typeof(string);
            authors.Columns.Add(col);

            DataColumn col2 = new DataColumn();
            col2.ColumnName = "InstruQuantity";
            col2.DataType = typeof(string);
            authors.Columns.Add(col2);

            DataColumn colcheck = new DataColumn("IsSelected", typeof(bool));
            authors.Columns.Add(colcheck);

            DataRow row1 = authors.NewRow();
            row1[0] = "Ganga";
            row1[1] = "10";
            authors.Rows.Add(row1);

            DataRow row2 = authors.NewRow();
            row2[0] = "Bhagya";
            row2[1] = "20";
            authors.Rows.Add(row2);
        }

        private bool _SelectAll;
        public bool SelectAll1
        {
            get { return _SelectAll; }
            set {
                    _SelectAll = value;
                    OnPropertyChanged("SelectAll1");
                    SelectAll();
                }
        }

        public void SelectAll()
        {
            if (SelectAll1 == true)
            {
                foreach (DataRow row in authors.Rows)
                {
                    row["IsSelected"] = true;
                }
            }
            else
            {
                foreach (DataRow row in authors.Rows)
                {
                    row["IsSelected"] = false;
                }
            }
        }
    }
}



请任何人可以帮助我,如何验证数量"文本框??



Please anyone can help me, how to validate Quantity Textbox??

推荐答案

您可能必须创建自定义控件(如NumericTextBox)并使用它而不是文本框.另一种处理预览文本更改事件并仅接受数字字符的方法.

请参考以下线程.

http://stackoverflow. com/questions/1268552/how-do-i-get-a-textbox-to-only-accept-numeric-input-in-wpf [ ^ ]

希望对您有所帮助!
You may have to create your custom control like NumericTextBox and use it instead of text box. Another way handle the preview text changed event and accept numeric chars alone.

Refer the following thread.

http://stackoverflow.com/questions/1268552/how-do-i-get-a-textbox-to-only-accept-numeric-input-in-wpf[^]

Hope it helps!


这篇关于Datagrid文本框验证,用于MVVM中的数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆