2012-11-21 3 views
1

전체 바인딩 된 개체가 변환기로 전달되는 ListBox가 있고 (필요한 경우) 개체가 올바르게 업데이트되지 않는 것 같습니다. 여기에 데이터 모델에서 관련 XAML변환기가있는 전체 개체를 텍스트로 바인딩

<TextBlock 
      Text="{Binding Converter={StaticResource DueConverter}}" 
      FontSize="{StaticResource PhoneFontSizeNormal}" 
      Foreground="{StaticResource PhoneDisabledBrush}" /> 

그리고 변환기

public class DueConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value == null) return null; 
     Task task = (Task)value; 
     if (task.HasReminders) 
     { 
      return task.Due.Date.ToShortDateString() + " " + task.Due.ToShortTimeString(); 
     } 
     else 
     { 
      return task.Due.Date.ToShortDateString(); 
     } 
    } 

    //Called with two-way data binding as value is pulled out of control and put back into the property 
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

그리고 마지막으로 인해 날짜 시간입니다.

private DateTime _due; 

    [Column(CanBeNull=false)] 
    public DateTime Due 
    { 
     get { return _due; } 
     set 
     { 
      if (_due != value) 
      { 
       NotifyPropertyChanging("Due"); 
       _due = value; 
       NotifyPropertyChanged("Due"); 
      } 
     } 
    } 

NotifyPropertyChanging/변경 작업, 다른 속성에 바인딩 된 다른 컨트롤이 제대로 업데이트 때문이다.

내 목표는 만기가 변경되었을 때 만기일 TextBlock을 업데이트하는 것입니다. 그러나 출력의 형식은 Task 개체의 다른 속성에 따라 다릅니다.

의견이 있으십니까?

+0

일반적으로 SO 프로토콜은 질문 제목에 태그를 넣지 않는 것입니다. 제목을 업데이트하고 질문에 다시 태그를 답니다. –

+0

그렇게 해 주셔서 감사합니다. – upopple

답변

0

TextBlock.Text 속성이 Task 인스턴스에 바인딩되어 있으므로 "Due"가 걱정되지 않습니다. "Due"값을 변경할 때마다 해당 바인딩이 자체적으로 업데이트되도록 "Task"에 대한 속성 변경 이벤트를 발생시켜야합니다.

+0

어떻게하면됩니까? private void NotifyPropertyChanged (string propertyName) { if (PropertyChanged! = null) { PropertyChanged (this, new PropertyChangedEventArgs (propertyName)); 자동으로 함수를 호출해서는 안됩니다. } }'datacontext에? – upopple

+0

"Due"와 같은 속성에 바인딩하면 코드가 setter에서 NotifyPropertyChanged를 호출하므로이 속성이 사용됩니다. 하지만 당신의 코드에서는 Task의 인스턴스가 아니라 그 인스턴스의 속성에 직접 바인딩됩니다. Task 인스턴스에 바인딩하는 방법에 따라 다르지만 Text = "{Binding Converter = {StaticResource DueConverter}}"자체를 업데이트하려면 코드에서 NotifyPropertyChanged ("name_of_your_task_property")를 발생시켜야합니다. ListBox의 ItemsSource로 사용되는 데이터를 어떻게 만들고 설정합니까? – trydis

관련 문제