【问题标题】:ListBox SelectedItem not working列表框 SelectedItem 不起作用
【发布时间】:2012-09-04 16:54:56
【问题描述】:

我正在尝试将对象列表加载为包含多个数据的网格,我已经为其选择了 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 之后会更长)。 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 问题之后,我得到了很多:

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

【问题讨论】:

  • LoadPeople 怎么称呼?是什么导致它被调用?
  • 为什么 Listbox 的 itemoruce 属性为 Mode=TwoWay ..尝试删除此
  • 您的输出窗口中是否有任何相关信息?
  • @zapico 我建议打开在抛出 CLR 异常时会中断的功能。如果您使用的是 Visual Studio 2010 或 2012,请转到 Debug -> Exceptions(或默认为 Ctrl-E、D)并在“Common Language Runtime Exceptions”下确保选中“Thrown”。然后重新运行您的解决方案,看看是否有任何问题。
  • 谢谢瑞恩!!!这是因为我的 Person 对象内部有问题!!!请将此评论作为答案发布,以便我可以选择它作为正确的答案;-)。再次感谢。

标签: c# silverlight mvvm


【解决方案1】:

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

按照 vinod8812 的建议,将绑定模式更改为 OneWay,或者将其删除,因为 OneWay 是默认设置:

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

更改在 ViewModel 中填充 ObservableCollection&lt;Player&gt; 的顺序:

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();
}

您知道ViewModel 上的INotifyPropertyChanged.PropertyChanged 事件有什么东西吗?从您分享的代码中并不清楚究竟是什么导致应用程序变慢。

【讨论】:

  • 感谢您的回答。 “双向”模式只是尝试查看它是否已“重新加载”并且已被删除。我认为 propertychanged 事件没有任何限制,但奇怪的是:每次我选择不同的事件时它都会增加。还有最奇怪的。我第一次选择一个项目时,绑定的属性被设置,但之后它并没有改变......
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2013-03-01
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多