将“选定日期”设置为TimePicker wpf(DataTemplate内的TimePickers)

本文关键字:wpf TimePicker DataTemplate 内的 TimePickers 设置 日期 选定日期 | 更新日期: 2023-09-27 18:27:35

我想要TimePicker的"选定日期"。当我从视图窗口中显示的日历中选择特定日期时,该日期应分配给Timepicker。

Xaml:

<DataTemplate x:Key="EditableDataTemplate">
  <StackPanel Orientation="Horizontal" Width="596">
   <xctk:TimePicker Name="StartPicker" Value="{Binding StartTime, Mode=TwoWay}" 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 EndTime, Mode=TwoWay}" Format="Custom" FormatString="hh:mm tt" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="60" EndTime="11:59:0"/>
  </StackPanel>
</DataTemplate>

以下属性更新TimePicker的时间,但设置今天的日期。我希望选择在运行时选择的日期。

private DateTime _started_at;
private DateTime _ended_at;
public DateTime StartTime
    {
        get
        {
            return _started_at;
        }
        set
        {
            _started_at = value;
        }
    }
    public DateTime EndTime
    {
        get
        {
            return _ended_at;
        }
        set
        {
            _ended_at = value;
        }
    } 

想法?

将“选定日期”设置为TimePicker wpf(DataTemplate内的TimePickers)

您正在绑定到不存在的属性。将绑定从StartValue和EndValue更改为StartTime和EndTime。

时间选择器:

public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; } 

XAML:

<DataTemplate x:Key="EditableDataTemplate">
  <StackPanel Orientation="Horizontal" Width="596">
   <xctk:TimePicker Name="StartPicker" Value="{Binding StartTime, 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 EndTime, 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>

EDIT在谷歌上做了一些急功近利的事情,意识到你没有任何财产变更的通知。请尝试添加INotifyPropertyChanged接口,并引发OnPropertyChanged事件以查看是否将其清除。onPropertyChanged 的示例