【求助】前辈们,请问一下:Silverlight的DataGrid控件中某列只能输入数字怎么做啊???

时间:2021-02-09 16:20:48
就是说DataGrid控件不是有很多列吗?有一列是可以自己输入数据的,而且要求输入的数据必须为数字,请问这怎么做啊??? 【求助】前辈们,请问一下:Silverlight的DataGrid控件中某列只能输入数字怎么做啊???

2 个解决方案

#1


最简单的是用数据绑定,当列绑定的类型是整形时,输入时自然就只能输入整形。

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="SilverlightApplication1111.MainPage"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">

        <sdk:DataGrid x:Name="dg1" AutoGenerateColumns="False" HorizontalAlignment="Left" Height="238" Margin="10,10,0,0" VerticalAlignment="Top" Width="314">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Header="此列只能是数字" Binding="{Binding MyNumber}"/>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>

    </Grid>
</UserControl>


using System;
using System.Collections.Generic;
using System.Windows.Controls;

namespace SilverlightApplication1111
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            this.dg1.ItemsSource = new List<MyTest>{
                new MyTest{ MyNumber=1}
            };
        }

        public class MyTest
        {
            public Int32 MyNumber { get; set; }
        }
    }
}


【求助】前辈们,请问一下:Silverlight的DataGrid控件中某列只能输入数字怎么做啊???

#2


引用 1 楼 KumaPower 的回复:
最简单的是用数据绑定,当列绑定的类型是整形时,输入时自然就只能输入整形。

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="SilverlightApplication1111.MainPage"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">

        <sdk:DataGrid x:Name="dg1" AutoGenerateColumns="False" HorizontalAlignment="Left" Height="238" Margin="10,10,0,0" VerticalAlignment="Top" Width="314">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Header="此列只能是数字" Binding="{Binding MyNumber}"/>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>

    </Grid>
</UserControl>


using System;
using System.Collections.Generic;
using System.Windows.Controls;

namespace SilverlightApplication1111
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            this.dg1.ItemsSource = new List<MyTest>{
                new MyTest{ MyNumber=1}
            };
        }

        public class MyTest
        {
            public Int32 MyNumber { get; set; }
        }
    }
}


【求助】前辈们,请问一下:Silverlight的DataGrid控件中某列只能输入数字怎么做啊???


非常非常感谢你~!!!

#1


最简单的是用数据绑定,当列绑定的类型是整形时,输入时自然就只能输入整形。

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="SilverlightApplication1111.MainPage"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">

        <sdk:DataGrid x:Name="dg1" AutoGenerateColumns="False" HorizontalAlignment="Left" Height="238" Margin="10,10,0,0" VerticalAlignment="Top" Width="314">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Header="此列只能是数字" Binding="{Binding MyNumber}"/>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>

    </Grid>
</UserControl>


using System;
using System.Collections.Generic;
using System.Windows.Controls;

namespace SilverlightApplication1111
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            this.dg1.ItemsSource = new List<MyTest>{
                new MyTest{ MyNumber=1}
            };
        }

        public class MyTest
        {
            public Int32 MyNumber { get; set; }
        }
    }
}


【求助】前辈们,请问一下:Silverlight的DataGrid控件中某列只能输入数字怎么做啊???

#2


引用 1 楼 KumaPower 的回复:
最简单的是用数据绑定,当列绑定的类型是整形时,输入时自然就只能输入整形。

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="SilverlightApplication1111.MainPage"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">

        <sdk:DataGrid x:Name="dg1" AutoGenerateColumns="False" HorizontalAlignment="Left" Height="238" Margin="10,10,0,0" VerticalAlignment="Top" Width="314">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Header="此列只能是数字" Binding="{Binding MyNumber}"/>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>

    </Grid>
</UserControl>


using System;
using System.Collections.Generic;
using System.Windows.Controls;

namespace SilverlightApplication1111
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            this.dg1.ItemsSource = new List<MyTest>{
                new MyTest{ MyNumber=1}
            };
        }

        public class MyTest
        {
            public Int32 MyNumber { get; set; }
        }
    }
}


【求助】前辈们,请问一下:Silverlight的DataGrid控件中某列只能输入数字怎么做啊???


非常非常感谢你~!!!