'observable'에 해당되는 글 1건

  1. 2011.01.06 [wp7] Observable Collection을 이용한 바인딩
Windows Phone 72011. 1. 6. 21:48

예전 포스트에서도 썻지만 약간 달라져서 다시 쓴다.

 

먼저 이런식으로 notifyPropertyChanged 이벤트 클래스를 하나만든다.

 

public class ItemCoinInfo : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
 
        public ItemCoinInfo()
        {
 
        }
    
        #region TypeProperty
        private ItemCoinType _type;
        /// <summary>
        /// 
        /// </summary>
        public ItemCoinType Type
        {
            get
            {
                return _type;
            }
            set
            {                
                if (value != _type)
                {
                    _type = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Type"));
                    }
                }
            }
        }
        #endregion TypeProperty            
    
        
        #region CountProperty
        private int _count;
        /// <summary>
        /// 
        /// </summary>
        public int Count
        {
            get
            {
                return _count;
            }
            set
            {
                if (value != _count)
                {
                    _count = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Count"));
                        PropertyChanged(this, new PropertyChangedEventArgs("DisplayName"));
                    }
                }
            }
        }
 
        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                if (value != _name)
                {
                    _name = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Name"));
                    }
                }
            }
        }
 
        private string _displayName;
        public string DisplayName
        {
            get
            {
                _displayName = string.Format("{0} X {1}", Name, Count);
                return _displayName;
            }           
        }
 
        #endregion MyPropertyProperty                
    }
 
그뒤에는
이것을  다시
public static class ItemList
  {
      public static ObservableCollection<ItemCoinInfo> ItemsInfo { get; set; }
 
      static ItemList()
      {
          ItemsInfo = new ObservableCollection<ItemCoinInfo>();
 
          if (ItemsInfo.Count == 0)
          {
              //todo : 현재는 강제로 넣고 있지만 나중에는 xml에서 읽어올것 without count
              //초기에만 넣는다는 것을 잊으면 안됨 나중에 확장을 생각할것 -jdlee
              ItemsInfo.Add(new ItemCoinInfo { Count = 0, Type = ItemCoinType.FC1, Name = "Fortune Coin" });
              ItemsInfo.Add(new ItemCoinInfo { Count = 0, Type = ItemCoinType.PC1, Name = "Pray Coin" });
              ItemsInfo.Add(new ItemCoinInfo { Count = 0, Type = ItemCoinType.WC1, Name = "Web Coin" });
          }
      }
  }

 

이렇게 만들고

여기 안의 count개수를 이런식으로 linq로 늘리면?

//아이템 개수를 하나 늘린다.          
               var type = (ItemCoinType)Enum.Parse(typeof(ItemCoinType), viewmodel.CoinInfo.Id, true);
               (from p in ItemList.ItemsInfo where p.Type == type select p).FirstOrDefault().Count += 1;              

 

자동으로 이것을 바인딩한 컨트롤은 바뀐다.

Posted by 동동(이재동)