Tuesday, September 4, 2012

ListBox SelectedItem not working

ListBox SelectedItem not working

I'm trying to load a list of objects as grids with several data and I've selected ListBox for it as you can see here:

<ListBox ItemsSource="{Binding People, Mode=TwoWay}" SelectedItem="{Binding Person, Mode=TwoWay}" >   <ListBox.ItemContainerStyle>      <Style TargetType="ListBoxItem">         <Setter Property="HorizontalContentAlignment" Value="Stretch" />      </Style>   </ListBox.ItemContainerStyle>   <ListBox.ItemTemplate>      <DataTemplate>          <Grid>              <Grid.ColumnDefinitions>                   <ColumnDefinition Width="200"></ColumnDefinition>                   <ColumnDefinition Width="50"></ColumnDefinition>                   <ColumnDefinition Width="30"></ColumnDefinition>                   <ColumnDefinition Width="50"></ColumnDefinition>                   <ColumnDefinition Width="20"></ColumnDefinition>              </Grid.ColumnDefinitions>              <TextBlock VerticalAlignment="Center" Text="{Binding name}" Grid.Column="0" TextAlignment="Right"></TextBlock>              <TextBox Grid.Column="1" Margin="5,0,0,0" Height="Auto" VerticalAlignment="Center" HorizontalAlignment="Stretch" Text="{Binding age}"></TextBox>              <TextBlock Grid.Column="2" Text="yo" VerticalAlignment="Center" Margin="5,0,0,0"></TextBlock>              <TextBox Grid.Column="3" Margin="5,0,0,0" Height="Auto" VerticalAlignment="Center" HorizontalAlignment="Stretch" Text="{Binding baremo}"></TextBox>              <TextBlock Grid.Column="4" Text="€" VerticalAlignment="Center" Margin="5,0,0,0"></TextBlock>          </Grid>     </DataTemplate>  </ListBox.ItemTemplate> 

When I load data into People ObservableCollection everything goes right and it is shown perfectly.

The first time I select a Person, it works and it sets the property value in the VM.

The problem comes the second time I select a different "Person". The property doesn't change and... every time I select a different one, it takes a little longer (after 10 รณ 20 it is MUCH LONGER).

Here is my ViewModel code:

#region People         public const string PeoplePropertyName = "People";          private ObservableCollection<Player> _People;          public ObservableCollection<Player> People         {             get             {                 return _People;             }              set             {                 _People = value;                 RaisePropertyChanged(People);             }         }         

No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog