簡體   English   中英

用轉換器將整個對象綁定到文本

[英]Binding entire object with converter to text

我有一個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 / Changed工作,因為綁定到不同屬性的其他控件正確更新。

我的目標是每當更改Due時都更新TextBlock的截止日期,但是輸出的格式取決於Task對象的另一個屬性。

有什么想法嗎?

TextBlock.Text屬性綁定到Task實例,它不在乎“到期”。 每當您更改“到期”的值時,還需要引發“任務”的屬性更改事件,以使綁定本身進行更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM