ã³ã³ããã¼ã©ã使ç¨ããã°ä¸ã¤ã®ã¿é¸æããã¨ãããã¨ãç°¡åã«ã§ãã¾ããããããC#ã³ã¼ãä¸ã§ããããã±ã¼ã¹ãããã¾ãããMVVMã§è¨ãModelå ã§æä»é¸æãè¡ãæ¹æ³ã
ããããããããã風ã«ã³ã¬ã¯ã·ã§ã³ãæ±ã£ã¦ããã®ãããããªãã®ã ãã©ã親ã¨åã®ã¯ã©ã¹ããã£ã¦ã³ã¬ã¯ã·ã§ã³ã®è¿½å ã親ã¯ã©ã¹ãéãã¦è¡ãããã ããé¸æç¶æ ã¯åãã¼ãããå¤æ´ã§ããããã«ãããã
åãã¼ã Item
namespace TestApp.Models { using System; using System.Collections.ObjectModel; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using Livet; /// <summary> /// æ¢åã¢ãã«åä½ã®æ å ± /// </summary> public class MovieFileItem : NotificationObject { public MovieFileItem(MovieFile parent) { unselectItem = parent.UnselectItem; } /// <summary> /// å å¼ã¢ã¤ãã ã®é¸æãåé¤ /// </summary> public Action unselectItem = null; /// <summary> /// é¸æããã¦ãããã©ãã /// </summary> private bool isSelected = true; /// <summary> /// é¸æããã¦ãããã©ãã /// </summary> public bool IsSelected { get { return isSelected; } set { if(value && !(isSelected)) { // æ°ãã«é¸æããå ´åã¯åé¤ if(unselectItem != null) unselectItem(); } isSelected = value; RaisePropertyChanged(() => IsSelected); } } } }
親ãã¼ã
namespace TestApp.Models { using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq.Expressions; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using Livet; /// <summary> /// æ¢åã¢ãã«åä½ã®æ å ± /// </summary> public class MovieFile : ObservableCollection<MovieFileItem>, INotifyPropertyChanged { public MovieFile() { } #region é¸æ /// <summary> /// å ¨ã¦é¸æ解é¤ãã /// </summary> public void UnselectItem() { foreach(var item in this) { item.IsSelected = false; } RaisePropertyChanged(() => SelectedItem); } /// <summary> /// æå®ãããã¢ã¤ãã ãé¸æãã /// </summary> /// <param name="selectItem">é¸æãããã¢ã¤ãã </param> public void SelectItem(MovieFileItem selectItem) { foreach(var item in this) { item.IsSelected = false; } selectItem.IsSelected = true; RaisePropertyChanged(() => SelectedItem); } /// <summary> /// é¸æãããã¢ã¤ãã /// </summary> private MovieFileItem selectedItem = null; /// <summary> /// é¸æãããã¢ã¤ãã /// </summary> public MovieFileItem SelectedItem { get { return selectedItem; } set { selectedItem = value; RaisePropertyChanged(() => SelectedItem); } } #endregion // é¸æ #region ObservableCollection override ã¡ã³ãã¼ protected override void ClearItems() { SelectedItem = null; base.ClearItems(); } protected override void RemoveItem(int index) { if(this[index].IsSelected) { SelectedItem = null; } base.RemoveItem(index); } protected override void SetItem(int index, MovieFileItem item) { if(this[index].IsSelected) { SelectedItem = null; } base.SetItem(index, item); } #endregion ObservableCollection override ã¡ã³ãã¼ #region INotifyPropertyChanged ã¡ã³ãã¼ public event PropertyChangedEventHandler PropertyChanged; protected void RaisePropertyChanged(string name) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name)); } protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression) { // Livetã®å®è£ if (propertyExpression == null) throw new ArgumentNullException("propertyExpression"); if (!(propertyExpression.Body is MemberExpression)) throw new NotSupportedException("ãã®ã¡ã½ããã§ã¯ ()=>ãããã㣠ã®å½¢å¼ã®ã©ã ãå¼ä»¥å¤è¨±å¯ããã¾ãã"); var memberExpression = (MemberExpression)propertyExpression.Body; RaisePropertyChanged(memberExpression.Member.Name); } #endregion } }
æ´æ°éç¥ã®RaisePropertyChangedã¯Livetã使ç¨ã親ãã¼ãã®æ´æ°éç¥ã¯Livetã³ã¼ããã³ããã