ListBox SelectedItem不起作用

时间:2012-09-04 16:54:57

标签: c# silverlight mvvm

我正在尝试将对象列表作为包含多个数据的网格加载,我已经为它选择了ListBox,如下所示:

<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>

当我将数据加载到People ObservableCollection中时,一切正常并且显示完美。

我第一次选择Person时,它可以,并在VM中设置属性值。

问题来自第二次我选择了另一个“人”。属性不会改变和... 每次我选择一个不同的属性时,需要更长的时间(在10 - 20之后它会更长)。

这是我的ViewModel代码:

#region People
        public const string PeoplePropertyName = "People";

        private ObservableCollection<Player> _People;

        public ObservableCollection<Player> People
        {
            get
            {
                return _People;
            }

            set
            {
                _People = value;
                RaisePropertyChanged(People);
            }
        }
        #endregion

        #region Person
        public const string PersonPropertyName = "Person";

        private Player _Person;

        public Player Person
        {
            get
            {
                return _Person;
            }

            set
            {
                _Person = value;
                RaisePropertyChanged(PersonPropertyName);
            }
        }
        #endregion

我正在加载人员列表:

void LoadPeople(GetPlayersEventArgs e)
{
     if (this.People == null) this.People = new ObservableCollection<Player>();

        foreach (Player Person in e.Result)
        {
            Player newPerson = new Player();
            ...
            this.People.Add(newPerson);
        }

        this.SetUnBusy();
}

我错过了什么吗?提前谢谢。

@Ryan问题之后我每次尝试选择其他值时都会看到,我得到了很多:

  

“System.InvalidCastException”类型的第一次机会异常   发生在解决方案

2 个答案:

答案 0 :(得分:2)

根据您的共享,Person属性的实现是好的。我会推荐几件事。

正如vinod8812建议的那样,将绑定模式更改为OneWay,或将其删除,因为OneWay是默认设置:

<ListBox ItemsSource="{Binding People}" SelectedItem="{Binding Person, Mode=TwoWay}" >

更改在ViewModel中填充ObservableCollection<Player>的顺序:

public class ViewModel
{
    private readonly ObservableCollection<Player>();

    public ViewModel()
    {
        _People = new ObservableCollection<Player>();
    }

    public ObservableCollection<Player> People
    {
        get
        {
            return _People;
        }
    }
    /* rest of class */
}

最后,在填充之前清除_People集合。否则你可能只是添加重复项:

void LoadPeople(GetPlayersEventArgs e)
{
    _People.Clear();

    foreach (Player Person in e.Result)
    {
        Player newPerson = new Player();
        ...
        this.People.Add(newPerson);
    }

    this.SetUnBusy();
}

您知道的任何内容与INotifyPropertyChanged.PropertyChanged上的ViewModel事件有关吗?从代码中可以清楚地分析出可能导致应用程序放缓的原因。

答案 1 :(得分:1)

我建议启用在抛出CLR异常时会中断的功能。如果您使用的是Visual Studio 2010或2012,请转到Debug - &gt;例外(或默认情况下为Ctrl-E,D)和“公共语言运行时异常”下的“例外”确保选中“Thrown”。然后重新运行您的解决方案,看看是否有任何问题。

然后你会在你的Person对象中看到问题;)