Reputation: 26312
This is my working code,
private ObservableCollection<User> _Users;
public ObservableCollection<User> Users
{
get { return _Users; }
set { _Users = value; RaisePropertyChanged(()=> Users); }
}
Users = new ObservableCollection<User>();
for (int i = 1; i <= 10; i++)
{
Users.Add(new User()
{
ADDRESS_LINE_1 = "Test Address",
ADDRESS_LINE_2 = "Test Address 2",
FIRST_NAME = "Test Name " + i,
SURNAME = "Test surname " +i,
DATE_OF_BIRTH = DateTime.Now.Date,
GENDER = "M",
MOBILE_PHONE_NUMBER = "+1100000",
EMAIL_ADDRESS = "[email protected]",
LAST_MODIFIED = DateTime.Now,
LOGIN_NAME ="operator.domain.com",
ItemIndex = Users.Count +1
});
}
earlier i am using
private List<User> _Users;
public List<User> Users
{
get { return _Users; }
set { _Users = value; RaisePropertyChanged(()=> Users); }
}
Users = new List<User>();
for (int i = 1; i <= 10; i++)
{
Users.Add(new User()
{
ADDRESS_LINE_1 = "Test Address",
ADDRESS_LINE_2 = "Test Address 2",
FIRST_NAME = "Test Name " + i,
SURNAME = "Test surname " +i,
DATE_OF_BIRTH = DateTime.Now.Date,
GENDER = "M",
MOBILE_PHONE_NUMBER = "+9100000",
EMAIL_ADDRESS = "[email protected]",
LAST_MODIFIED = DateTime.Now,
LOGIN_NAME ="operator1.domain.com",
ItemIndex = Users.Count +1
});
}
and i am continuously getting following exception message, when i try to bind it with datatemplate in xaml
Value does not fall within the expected range.
Stack trace is null :(
In addition, if i take another list variable and add those values to it and then assign shallow copy of that list to the my above list, the code give me desired result, but again what actually going around.
This is the datatemplate
<DataTemplate>
<Grid Margin="-8,-10,-8,-10" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Style="{StaticResource BorderStyleForAdmin}" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<StackPanel Orientation="Horizontal">
<!--<Border BorderBrush="Gray" BorderThickness="0.3" Visibility="{Binding IsSelectedItem, Mode=TwoWay, Converter={StaticResource ControlVisibiltyOfSelectedItemInListBox}}">
<TextBlock Text="{StaticResource ArrowGlyph}" Foreground="Black" VerticalAlignment="Center" HorizontalAlignment="Right"></TextBlock>
</Border>-->
<TextBlock Style="{StaticResource TextBlockCell}" Text="{Binding SURNAME}"></TextBlock>
</StackPanel>
</Border>
<Border Grid.Column="1" Style="{StaticResource BorderStyleForAdmin}" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<TextBlock Style="{StaticResource TextBlockCell}" Text="{Binding FIRST_NAME}"></TextBlock>
</Border>
<Border Grid.Column="2" Style="{StaticResource BorderStyleForAdmin}" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<TextBlock Style="{StaticResource TextBlockCell}" HorizontalAlignment="Stretch">
<Run Text="{Binding ADDRESS_LINE_1}"></Run><LineBreak></LineBreak>
<Run Text="{Binding ADDRESS_LINE_2}"></Run>
</TextBlock>
</Border>
<Border Grid.Column="3" Style="{StaticResource BorderStyleForAdmin}" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<TextBlock Style="{StaticResource TextBlockCell}" Text="{Binding DATE_OF_BIRTH}"></TextBlock>
</Border>
<Border Grid.Column="4" Style="{StaticResource BorderStyleForAdmin}" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<TextBlock Style="{StaticResource TextBlockCell}" Text="{Binding EMAIL_ADDRESS}"></TextBlock>
</Border>
<Border Grid.Column="5" Style="{StaticResource BorderStyleForAdmin}" Background="{Binding ItemIndex, Converter={StaticResource AlternateRowBackgroundConverter}}">
<TextBlock Style="{StaticResource TextBlockCell}" Text="{Binding MOBILE_PHONE_NUMBER}"></TextBlock>
</Border>
</Grid>
</DataTemplate>
Though, Observable collection works for me but i am wondering why List giving me this exception, i am unable to figure out what's the actual reason behind the scene. Thanks in advance.
Upvotes: 1
Views: 977
Reputation: 20111
Only thing which bugs me out here is "RaisePropertyChanged
" is used with ObservableCollection
or at least you must inherit INotifyPropertyChanged
interface. In your second implementation list is not getting "RaisePropertyChanged" method. Consider list declaration as:
private List<User> _Users;
public List<User> Users
{
get { return _Users; }
set { _Users = value;}
}
I think this should resolve the error. But NOTE that doing so will not raise PropertyChanged event whenever the state of the object changes (Added, Removed, and Modified) to the point where you want to notify the underlying collection or container that the state has changed.
For more info read: List vs ObservableCollection vs INotifyPropertyChanged in Silverlight
Upvotes: 1