我正在尝试将对象列表加载为包含多个数据的网格,并且我已经为它选择了 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”类型的第一次机会异常