Solved:
The issue was in my ItemSource in XAML. I changed ItemsSource="{Binding Source=vlan}"
to ItemsSource="{Binding vlan}"
and it start displaying all the data in my Datagrid.
:)
Question
Hi guys i have a problem since last week and i am unable to sort out the reason,the problem is i want my textboxes data to be display in a datagrid ,i am using WPF C# and pattern is MVVM.
My code in XAML is:
<TextBox
Grid.Row="0"
Grid.Column="1"
Margin="0,5,0,0"
Height="25"
Foreground="Black"
Opacity="0.8"
Width="Auto"
Text="{Binding VlanName, UpdateSourceTrigger=PropertyChanged}"
/>
<TextBox
Grid.Row="1"
Grid.Column="1"
Margin="0,5,0,0"
Height="25"
Foreground="Black"
Opacity="0.8"
Width="70"
HorizontalAlignment="Left"
Text="{Binding VlanID, UpdateSourceTrigger=PropertyChanged}"
/>
<Button Grid.Column="3"
Grid.Row="1"
Content="Add VLAN"
Margin="10,5,0,0"
Style="{StaticResource AppButtons}"
Command="{Binding AddVlan}"
/>
<Button Grid.Column="3"
Grid.Row="2"
Content="Remove VLAN"
Margin="10,5,0,0"
Style="{StaticResource AppButtons}"
Width="100"
Command="{Binding RemoveVlan}"
/>
<DataGrid Grid.Row="4"
Grid.ColumnSpan="3"
Height="200"
Margin="10,10,0,0"
Name="dg"
ItemsSource="{Binding Source=vlan}"
AutoGenerateColumns="False"
>
<DataGrid.Columns>
<DataGridTextColumn Header="S.No" Binding="{Binding Path=S_No}"/>
<DataGridTextColumn Header="VLAN Name" Binding="{Binding Path=vname}" />
<DataGridTextColumn Header="VLAN ID" Binding="{Binding Path=vid}" />
<DataGridTextColumn Header=" IP" Width="100"/>
<DataGridTextColumn Header="VLAN Ports" Width="100"/>
</DataGrid.Columns>
</DataGrid>
C# View model is :
vlan = new ObservableCollection<VLANSPropertyClass>();
public string VlanName
{
get
{
return this.ConfigurationLibrary.ConfigLibraryVlanName;
}
set
{
if (String.Equals(this.ConfigurationLibrary.ConfigLibraryVlanName, value))
{
return;
}
this.ConfigurationLibrary.ConfigLibraryVlanName = value;
this.OnPropertyChanged("VlanName");
}
}
public string VlanID
{
get
{
return this.ConfigurationLibrary.ConfigLibraryVlanName;
}
set
{
if (String.Equals(this.ConfigurationLibrary.ConfigLibraryVlanName, value))
{
return;
}
this.ConfigurationLibrary.ConfigLibraryVlanName = value;
this.OnPropertyChanged("VlanID");
}
}
public ICommand AddVlan
{
get
{
if (_addVlan == null)
_addVlan = new RelayCommand(() => this.AddVlans());
return _addVlan;
}
}
public ICommand RemoveVlan
{
get
{
if (_removeVlan == null)
_removeVlan = new RelayCommand(() => this.RemoveVlans());
return _removeVlan;
}
}
#endregion //ICommand region end
void AddVlans()
{
Console.WriteLine("Add vlan");
var serial = new VLANSPropertyClass();
serial.S_No = vlan.Count + 1;
serial.vname = VlanName;
serial.vid = VlanID;
vlan.Add(serial);
}
Code in the VLAN method is not working i dont know why because i am able to see the line Add vlan
which i wrote in Console but later code is not working i dont know the reason please help?
Solution
Solved:
The issue was in my ItemSource in XAML. I changed ItemsSource="{Binding Source=vlan}"
to ItemsSource="{Binding vlan}"
and it start displaying all the data in my Datagrid.
:)