您好我正在处理一个简单的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; }
}
}
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>
Snap是:
谁能告诉我我在哪里犯错误。 任何帮助都会受到高度赞扬。
答案 0 :(得分:0)
问题出在你的事件处理程序上,而不是你尝试过这个
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);
}