Windows Phone 72012. 1. 3. 21:20

예를들어 하위 UserControl 안에

public double CurrentCount { get; set; }

 

이런 property가 있다고 하자. 이것을 하위 View에서 보고 싶다. 그럴때는?

바인딩을 이렇게 하면 된다.

  <ct:SelectPersonControl Margin="10" x:Name="SPC"/>
<TextBlock Text="{Binding Path=CurrentCount, ElementName=SPC}" />

이렇게 하면 UserControl의 CurrentCount값이 바인딩 된다.

 

너무 기초인가… 혹시나 하는 마음에 알아두길~

Posted by 동동(이재동)
Windows Phone 72012. 1. 3. 21:16

회사에서 ViewModelBase(회사에서 쓰는 ViewModel dll)을 이용한다.

하지만 이건 ViewModel만들때 상속해서 써야 한다.

하지만 UserControl을 만들어 버리면 이미 UserControl을 상속받았기 때문에 ViewModelBase를 다중상속받아야 한다.

하지만 Property Binding이 목적이라면 이렇게도 쓸 수 있다.

 

#region CurrentCount

      private double _currentCount;

      /// <summary>
      ///
      /// </summary>
      public double CurrentCount
      {
          get
          {
              return _currentCount;
          }
          set
          {
              _currentCount = value;
              
              OnPropertyChanged("CurrentCount");
          }
      }

      #endregion CurrentCount

      #region Event

      /// <summary>
      /// PropertyChanged 이벤트 핸들러.
      /// </summary>
      public event PropertyChangedEventHandler PropertyChanged;

      /// <summary>
      /// OnPropertyChanged()
      /// </summary>
      /// <param name="propertyName"></param>
      protected void OnPropertyChanged(string propertyName)
      {
          if (PropertyChanged == null)
              return;
          PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }

      #endregion Event

이렇게 이벤드 핸들러를 만들어서 OnPropertyChanged를 구현한다.

Posted by 동동(이재동)
Windows Phone 72011. 12. 7. 14:10

Custom UserControl을 만들고 안에 TextBox를 하나 넣었다고 가정하자

여기 TextBox에 Text를 바꾸기 위해서는 어떻게 하는게 좋을까?

Behind에서 그냥 강제로 바꾸는것이 좋을까? 아니다 그거보다 쉬운방법이 있다.

바로 DataContext에 넣어서 바인딩 하는 방법이다.

일단 유저컨트롤을 만들고 상위 Grid의 이름을 정하자 기본적으로 "LayoutRoot" 로 지정되어 있을것이다.

그리고 비하인드에서 의존 프로퍼티를 하나 만들자. 이름은 그냥 ItemSource로 지었다

#region ItemSource

       /// <summary>
       /// Gets or sets the ItemSource possible Value of the TransferTrnInfoModel object.
       /// </summary>
       public TrnInfoModel ItemSource
       {
           get { return (TrnInfoModel)GetValue(ItemSourceProperty); }
           set { SetValue(ItemSourceProperty, value); }
       }

       /// <summary>
       /// Identifies the ItemSource dependency property.
       /// </summary>
       public static readonly DependencyProperty ItemSourceProperty =
                   DependencyProperty.Register(
                         "ItemSource",
                         typeof(TrnInfoModel),
                         typeof(GeneralTrainInfoControl),
                         new PropertyMetadata(OnItemSourcePropertyChanged));

       /// <summary>
       /// ItemSourceProperty property changed handler.
       /// </summary>
       /// <param name="d">TrainInfoControl that changed its ItemSource.</param>
       /// <param name="e">DependencyPropertyChangedEventArgs.</param>
       private static void OnItemSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
       {
           GeneralTrainInfoControl _TrainInfoControl = d as GeneralTrainInfoControl;
           if (_TrainInfoControl != null)
           {
               _TrainInfoControl.SetItemSource((TrnInfoModel)e.NewValue);
           }
       }

       #endregion ItemSource

       private void SetItemSource(TrnInfoModel param)
       {
           LayoutRoot.DataContext = param;
       }

그리고 source를 받으면

LayoutRoot.DataContext = param;

이렇게 dataContext에 넣게 바꿨다.

TrnInfoModel은 모델 클래스이고

<ct:GeneralTrainInfoControl ItemSource="{Binding GeneralTrainInfoSource}">

이런식으로 사용 하면 된다.

물론 모델 클래스에는 값이 다 정의 해서 넘겨줘야 한다.

이렇게 하면 해당 UserControl에서 Text같은곳에 바인딩만 걸어주면 끝~

구지 textbox.Text = TrnInfoModel.name

이런식으로 일일히 하지 않아도 되는것이다.

그리고 그냥 usrcontrol만들고 그안에 text만 binding할려면 젤 쉬운방법은

위처럼 궂이 Itemsource 의존 프로퍼티를 만들지 않고

그냥 DataContext에 넣으면 된다.

Posted by 동동(이재동)