如果输入文本,Datepicker不会更新属性 [英] Datepicker is not updating the property if input the text

查看:98
本文介绍了如果输入文本,Datepicker不会更新属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似

DatePicker SelectedDate="{Binding Path=BeginDate, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>

然后在UI上,如果输入值,则不会命中setter部分。

Then on UI, if I input the value, the setter part is not hit.

private DateTime? _beginDate;
public DateTime?
{
 get{return _beginDate;}
 set{ _beginDate = value; PropertyChange();}

所以我决定使用转换器 

So I decide to use converter 

<DatePicker 
     Text="{Binding BeginDate,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
     SelectedDate="{Binding RelativeSource={RelativeSource Self},Path=Text,
     Converter={StaticResource DateConverter}}">


 

然而它抛出了StackOverflow异常,因为它是循环引用。

 

However it throw StackOverflow exception, because it is a circular reference.

更新:如果我使用Text绑定加转换器。然后我发现了一个有趣的东西,它需要一个ConvertBack方法。它还会创建一个循环引用然后内存溢出...

Update: if I use Text binding plus a converter. Then I found an interesting thing, which is it needs a ConvertBack method. It also creates a circular reference then memory overflow...







推荐答案

b $ b我无法重现你的问题。我认为DataContext没有设置。

Hi,
I cannot reproduce your problem. I think that the DataContext ist not set.

XAML:

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
  <StackPanel x:Name="sp">
    <DatePicker SelectedDate="{Binding Path=BeginDate, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
    <Label Content="{Binding BeginDate}"/>
  </StackPanel>
</Window>

和CodeBehind:

And CodeBehind:

Imports System.ComponentModel
Imports System.Runtime.CompilerServices

Class MainWindow
  Implements INotifyPropertyChanged

  Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
    Me.sp.DataContext = Me
  End Sub

  Private _par As Date?

  Public Property BeginDate As Date?
    Get
      Return Me._par
    End Get
    Set(value As Date?)
      Me._par = value
      OnPropChanged()
    End Set
  End Property

  Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
  Private Sub OnPropChanged(<CallerMemberName> Optional propName As String = "")
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
  End Sub

End Class

C#中的CodeBehind

And CodeBehind in C#

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;

namespace WpfApp1
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window, INotifyPropertyChanged
  {
    public MainWindow()
    {
      InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e) =>       this.sp.DataContext = this;

    private DateTime? _par;

    public DateTime? BeginDate
    {
      get => this._par;
      set
      {
        this._par = value;
        OnPropChanged();
      }
    }

    #region  OnPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropChanged([CallerMemberName] string propName = "") =>
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
    #endregion
  }
}


这篇关于如果输入文本,Datepicker不会更新属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆