Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Wpf datagrid自动增量列生成错误的值?_C#_Wpf_Xaml_Datagrid - Fatal编程技术网

C# Wpf datagrid自动增量列生成错误的值?

C# Wpf datagrid自动增量列生成错误的值?,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,您好,我正在处理一个简单的wpf应用程序。我的问题是我想包括序列号(S_No)列,该列应该自动递增,但它显示异常值。 我想要合适的序列列作为我的第一列。 我的代码是: public partial class MainWindow : Window { public ObservableCollection<VLANS> vlan { get; set; } public MainWindow() { I

您好,我正在处理一个简单的wpf应用程序。我的问题是我想包括序列号(S_No)列,该列应该自动递增,但它显示异常值。 我想要合适的序列列作为我的第一列。 我的代码是:

public partial class MainWindow : Window
    {
        public ObservableCollection<VLANS> vlan { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            vlan=new ObservableCollection<VLANS>();
            this.DataContext=this;



            dg.ItemsSource =vlan;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var serial = new VLANS();
            serial.S_No = vlan.Count;
            vlan.Add(serial);

            var vname = new VLANS();
            vname.VlanName = t1.Text;
            vlan.Add(vname);



        }
    }

    public class VLANS
    {
        public string VlanName { get; set; }
        public int S_No { get; set; }
    }
}
公共部分类主窗口:窗口
{
公共ObservableCollection vlan{get;set;}
公共主窗口()
{
初始化组件();
vlan=新的ObservableCollection();
this.DataContext=this;
dg.ItemsSource=vlan;
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
var serial=新的VLAN();
serial.S_No=vlan.Count;
添加(串行);
var vname=新的VLAN();
vname.vlaname=t1.Text;
添加(vname);
}
}
公共类VLAN
{
公共字符串VLAN名称{get;set;}
公共整数S_No{get;set;}
}
}
XAML是:

<Window x:Class="TextboxToDatagridTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition Height="200"/>
        </Grid.RowDefinitions>
        <TextBox
            Name="t1"
            Grid.Row="0"
            Width="150"
            Height="50"
            Margin="200,0,0,0"
            />
        <Button
            Grid.Row="0"
            Width="150"
            Height="40"
            Content="Button" FontSize="25"
            HorizontalAlignment="Left"
            Margin="80,0,0,0" Click="Button_Click">
         </Button>
        <DataGrid
            AutoGenerateColumns="False"
            Name="dg"
            Grid.Row="1">
            <DataGrid.Columns >
                <DataGridTextColumn Header="S.No" Binding="{Binding Path=S_No}" />
                <DataGridTextColumn Header="Vlan Name" Binding="{Binding Path=VlanName}"/>

            </DataGrid.Columns>

        </DataGrid>  

    </Grid>
</Window>

快照是:

谁能告诉我我错在哪里了吗。
任何帮助都会受到高度赞扬。

问题在于您的事件处理程序,而不是您尝试过的

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var serial = new VLANS();
        vlan.Add(serial)
        serial.S_No = vlan.Count;
        serial.VlanName = t1.Text;
        vlan.Add(serial);

    }