我正在尝试将对象列表加载为包含多个数据的网格,并且我已为其选择了 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 时,一切正常并且显示完美。
我第一次选择一个人时,它起作用并在 VM 中设置属性值。
问题第二次出现 我选择了一个不同的“人”。该属性不会改变并且...每次我选择一个不同的属性时,它都会花费更长的时间(在 10 ó 20 之后它会更长)。 p>
这是我的 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 问题之后,我得到很多:
A first chance exception of type 'System.InvalidCastException' occurred in Solution
最佳答案
根据您分享的 Person
的实现情况属性(property)不错。我会推荐一些东西。
按照vinod8812的建议,将绑定(bind)模式改为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();
}
你知道有什么是绑定(bind)到 INotifyPropertyChanged.PropertyChanged
的吗?您的 ViewModel
上的事件?从您分享的代码中,我们并不清楚到底是什么导致了应用程序变慢。
关于c# - ListBox SelectedItem 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12268053/