【问题标题】:UWP x:Bind with timeUWP x:与时间绑定
【发布时间】:2015-09-22 22:05:55
【问题描述】:

我将我的应用程序从 WP 8.1 / W 8.1 转换为 UWP。它包括一个更新文本框值的计时器。这是 XAML:

Text="{Binding CurrentLocalDateTime, Mode=TwoWay, Converter={StaticResource DateTimeConverter}}"

还有数据上下文:

    private DateTime currentLocalDateTime;

    public DateTime CurrentLocalDateTime { get { return currentLocalDateTime; } set { currentLocalDateTime = value; OnPropertyChanged("CurrentLocalDateTime"); } }

    private void TimerExecution(object sender, object e)
    {
        CurrentLocalDateTime = DateTime.Now;
    }

我想使用新的 x:Bind 绑定方式,但控件永远不会更新,使用以下代码:

Text="{x:Bind CurrentLocalDateTime, Mode=TwoWay, Converter={StaticResource DateTimeConverter}}"

数据上下文:

    public DateTime CurrentLocalDateTime { get; set; }

    private void TimerExecution(object sender, object e)
    {
        CurrentLocalDateTime = DateTime.Now;
    }

怎么了?

非常感谢您的帮助。 问候

【问题讨论】:

    标签: c# windows binding windows-10 uwp


    【解决方案1】:

    当您设置CurrentLocalDateTime 时,不会通知您的用户界面它发生了。它适用于第一种情况,因为您正在实现 INotifyPropertyChanged 并使用属性名称调用 OnPropertyChanged。

    public DateTime CurrentLocalDateTime 
    { 
        get { return currentLocalDateTime; }  
        set 
        { 
            currentLocalDateTime = value;
            OnPropertyChanged("CurrentLocalDateTime"); 
        } 
    }
    

    【讨论】:

    • 工作正常,谢谢。我认为 x:Bind 不需要 OnPropertyChanged,但忘了测试它;-)。
    • @RomEh 如果回答了您的问题,则标记为答案;)
    猜你喜欢
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    • 2017-02-01
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多