简体   繁体   English

绑定ListView.SelectedItem时遇到问题

[英]Trouble in binding ListView.SelectedItem

In my WPF mvvm(light) project I have such problem : after binding of ListView.SelectedItem to my ModelView , I'm trying to change it from ModelView .It seems to be ok, but nothing happend at View : 在我的WPF MVVM(光)的项目,我有这样的问题:结合后ListView.SelectedItem我的ModelView ,我试图把它从改变ModelView 。它似乎是确定的,但没有happend在查看:

XAML : XAML:

<ListView SelectedItem="{Binding SelectedOne}" 
          ItemsSource="{Binding Items}">
</ListView>

<ListView     ScrollViewer.CanContentScroll="False"
              ItemsSource="{Binding Items}"
              ItemTemplateSelector="{StaticResource myDataTemplateSelector}"
              HorizontalContentAlignment="Stretch"
              SelectedItem="{Binding SelectedOne, Mode=TwoWay}"
              IsSynchronizedWithCurrentItem="True">
</ListView>

MOdelView : MOdelView:

ObservableCollection<ItemViewModel> _EAItems = new ObservableCollection<ItemViewModel>();
public ObservableObject _selectedOne;

public ObservableCollection<ItemViewModel> Items
        {
            get
            {
                return _EAItems;
            }
            set
            {
                _EAItems = value;
            }
        }

public ObservableObject SelectedOne 
        {
            get { return _selectedOne; }
            set 
            {

                if(_selectedOne != value)
                _selectedOne = value;

            }
        }
Select = new RelayCommand(() => 
            {
                if (qw == 15) { qw = 0; }else
                SelectedOne = Items[qw];
                qw++;
            });

Items is ObservableCollection . Items是ObservableCollection

I added another Listview and bind it to the same sources. 我添加了另一个Listview并将其绑定到相同的源。 When I change SelctedItem in one of Listview , it's displayed on another , vise versa. 当我在Listview之一中更改SelctedItem时,它显示在另一个上,反之亦然。

I looked thru alot similar solutions, and I can't figure what is wrong :( 我看了很多类似的解决方案,但我不知道出了什么问题:(

first you should also post the code for the binding for the ItemsSource, the ItemsSource property. 首先,您还应该发布ItemsSource属性的绑定代码。

but the main problem is the you do not call PropertyChanged in your setter 但主要问题是您没有在设置器中调用PropertyChanged

    public ObservableObject SelectedOne 
    {
        get { return _selectedOne; }
        set 
        {

            if(_selectedOne != value)
            _selectedOne = value;

            OnPropertyChanged("SelectedOne ");//<-- otherwise the view dont know that the SelectedItem changed
        }
    }

In order to make the binding work you would have to raise a PropertyChanged event from the SelectedOne setter. 为了使绑定工作,您必须从SelectedOne设置器中引发PropertyChanged事件。 The class that defines the SelectedOne property would have to implement INotifyPropertyChanged . 定义SelectedOne属性的类必须实现INotifyPropertyChanged

Regardless of the type of the SelectedOne property (even if it is itself an ObservableObject), you have to raise a PropertyChanged event when the value of the property changes. 不管SelectedOne属性的类型如何(即使它本身是ObservableObject),也必须在PropertyChanged 更改时引发PropertyChanged事件。

I agree with Clemens and blindmeis. 我同意克莱门斯和布莱德梅斯的看法。 The only fact is that the SelectedOne need to be the same class that the one set for the Collection. 唯一的事实是,SelectedOne必须与为集合设置的类相同。 And with the MVVM-light libraries, the method is RaisePropertyChanged instead of OnPropertyChanged (if your viewModel inherit from ViewModelBase). 对于MVVM-light库,该方法为RaisePropertyChanged而不是OnPropertyChanged(如果您的viewModel继承自ViewModelBase)。

If your source contains ItemViewModels should use this code : 如果您的源包含ItemViewModels,则应使用以下代码:

private ItemViewModel _selectedOne;

public ItemViewModel SelectedOne 
{
    get { return _selectedOne; }
    set 
    {

        if(_selectedOne != value)
        _selectedOne = value;

        RaisePropertyChanged("SelectedOne");
    }
}

The reason why your collection don't need RaisePropertyChanged is that ObsvervableCollection class already contains it in a certain way. 您的集合不需要RaisePropertyChanged的原因是ObsvervableCollection类已经以某种方式包含它。

Regards, 问候,

Kévin 凯文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM