两类和未知类型之间的变量引用

时间:2015-12-09 16:54:34

标签: c# wpf mvvm ref

我有MainViewModel,其中包含我的所有绑定。特别是我的listView(多个listView与不同类型的绑定)。我想创建一个包含绑定变量的类,如(ItemSeletedItemIndexSelectedItemSource)。所以我写了这个:

public class ListViewContener<T>
    {
        public T ItemSelected { get; set; }
        public ObservableCollection<T> ItemSourceList { get; set; }
        public int ItemIndexSelected { get; set; }

        public void SetItem(ref T itemSelected, ref ObservableCollection<T> itemSourceList, ref int itemIndexSelected)
        {
            this.ItemSelected = itemSelected;
            this.ItemSourceList = itemSourceList;
            this.ItemIndexSelected = itemIndexSelected;
        }

        public void ToPrevious(MediaElement mediaElement, ImageSource imageSource)
        {
            Console.WriteLine(ItemIndexSelected);
            if (ItemIndexSelected - 1 >= 0)
            {
                ItemIndexSelected -= 1;
                dynamic dynSelected = ItemSelected;
                ItemSelected = ItemSourceList[ItemIndexSelected];
                mediaElement.Source = new Uri(dynSelected.Path);
                mediaElement.Play();
                imageSource = new BitmapImage(new Uri("../Ressources/play.png", UriKind.Relative));
            }
        }
    }

我的第一个问题是:

  • 我可以创建类似public ListViewContener<T>(ref T item ...)

  • 的构造函数
  • 我可以在我的班级中存储变量的引用,例如this.ItemSelected = itemSelected;

在我的MainViewModel中,我像这样调用函数SetItem(...)

this._musiqueList.SetItem(ref _selectedItemListMusique, ref _itemSourceListMusique, ref _itemIndexSelectedM);

示例绑定:

private MusicDB _selectedItemListMusique;
public MusicDB SelectedItemListMusique
        {
            get { return _selectedItemListMusique; }
            set
            {
                _selectedItemListMusique = value;
                this.OnPropertyChanged("SelectedItemListView");
            }
        }

0 个答案:

没有答案