3

我正在尝试将对象列表加载为包含多个数据的网格,并且我已经为它选择了 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 中时,一切正常,并且显示完美。

我第一次选择一个人时,它可以工作并在虚拟机中设置属性值。

当我第二次选择不同的“人”时,问题就来了。属性不会改变,而且...每次我选择不同的属性时,都需要更长的时间(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”类型的第一次机会异常

4

2 回答 2

2

从您分享的内容来看,该Person属性的实现是好的。有几件事我会推荐。

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

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

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

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吗?从您共享的代码中尚不清楚究竟是什么导致应用程序变慢。

于 2012-09-04T18:29:05.520 回答
1

我建议打开在抛出 CLR 异常时会中断的功能。如果您使用的是 Visual Studio 2010 或 2012,请转到 Debug -> Exceptions(或默认为 Ctrl-E、D)并在“Common Language Runtime Exceptions”下确保选中“Thrown”。然后重新运行您的解决方案,看看是否有任何问题。

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

于 2012-09-04T19:30:07.113 回答