1

在我的 Wpf 应用程序中,我有两个 TimePicker。我已经为他们使用了绑定,但他们的时间没有更新。我还想为这些 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

请提出一些想法?

4

1 回答 1

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 事件,只要您的绑定正确,您就会看到值更新。

于 2013-08-14T07:32:52.397 回答