将整个对象与转换器绑定到文本

时间:2012-11-21 21:18:55

标签: c# binding converter windows-phone

我有一个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();
    }
}

最后来自数据模型的到期DateTime。

private DateTime _due;

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

NotifyPropertyChanging / Changed工作,因为绑定到不同属性的其他控件正确更新。

我的目标是在更改Due时更新截止日期TextBlock,但输出的格式取决于Task对象的另一个属性。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

TextBlock.Text属性绑定到Task实例,它不关心“Due”。每当您更改“Due”的值时,您还需要为“Task”引发属性更改事件,以便该绑定更新自身。

相关问题