WPF- TimePicker绑定以在运行时设置日期和时间

时间:2013-08-14 07:03:33

标签: c# wpf datatemplate timepicker

在我的Wpf应用中,我有两个TimePickers。我已经使用了绑定,但他们的时间没有更新。我还想为这些TimePickers设置选定的日期。我试图将其绑定如下。但是,它不会起作用。这里的实际问题是TimePickers在DataTemplate中。

这是xaml:

<DataTemplate x:Key="EditableDataTemplate">
  <StackPanel Orientation="Horizontal" Width="596">
    <xctk:TimePicker Name="StartPicker" Value="{Binding StartValue, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" Format="Custom" FormatString="hh:mm tt" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="100" EndTime="11:59:0"/>
    <xctk:TimePicker Name="EndPicker" Value="{Binding EndValue, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" Format="Custom" FormatString="hh:mm tt" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="60" EndTime="11:59:0"/>
  </StackPanel>
</DataTemplate>

在C#中使用适当的绑定:

    private DateTime _dateTime1;
    public DateTime StartValue
    {
        get
        {
            return _dateTime1;
        }
        set
        {
            _dateTime1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
            OnPropertyChanged("StartValue");
        }
    }

    private DateTime _dateTime2;
    public DateTime EndValue
    {
        get
        {
            return _dateTime2;
        }
        set
        {
            _dateTime2 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
            OnPropertyChanged("EndValue");
        }
    }

    protected virtual void OnPropertyChanged(String time)
    {
        if (System.String.IsNullOrEmpty(time))
        {
            return;
        }
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(time));
        }
    }

    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion

请提出一些想法?

1 个答案:

答案 0 :(得分:0)

您正试图在属性设置器中设置永远不会被调用的值,因此永远不会设置属性值(因此永远不会被通知,并且TimePicker永远不会更新)。

为什么不尝试在ViewModel的构造函数中设置属性。我不确定你的ViewModel被调用了什么,但是这里是:

public class MyViewModel : INotifyPropertyChanged
{

public MyViewModel()
{
    StartValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
    EndValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
}

private DateTime _dateTime1;
public DateTime StartValue
{
    get
    {
        return _dateTime1;
    }
    set
    {
        if (_dateTime1.Equals(value)) return;
        _dateTime1 = value; 
        OnPropertyChanged("StartValue");
    }
}

private DateTime _dateTime2;
public DateTime EndValue
{
    get
    {
        return _dateTime2;
    }
    set
    {
        if (_dateTime2.Equals(value)) return;
        _dateTime2 = value;
        OnPropertyChanged("EndValue");
    }
}

protected virtual void OnPropertyChanged(String time)
{
    if (System.String.IsNullOrEmpty(time))
    {
        return;
    }
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(time));
    }
}

#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}

这将确保为两个属性触发PropertyChanged事件,只要您的绑定正确,您将看到值更新。