【问题标题】:Bind DateTime to Date and Time EditFields将 DateTime 绑定到日期和时间 EditFields
【发布时间】:2009-04-02 08:45:14
【问题描述】:

我正在尝试构建一个包含编辑对象的 DateTime 值的 gui。 DateTime 属性已绑定到时间的 DataPicker 和普通 TextBox。 当我更改时间文本框中的值时,日期时间属性中写入的值是今天输入的时间,而不是仅仅更新时间,保留原始日期。

如何实现只更改 DateTime 时间而不更改日期的 Time TextBox?

当前绑定:

<TextBlock>Time</TextBlock>
<TextBox Template="{StaticResource RoundedTextBoxTemplate}">
    <Binding Path="Delivery.Date" StringFormat="HH:mm">
        <Binding.ValidationRules>
            <v:IsValidTimeRule />
        </Binding.ValidationRules>
    </Binding>
</TextBox>

【问题讨论】:

    标签: c# .net wpf datetime data-binding


    【解决方案1】:

    我能想到的唯一方法是拥有一个 DateTime 属性,您只允许 setter 更改时间。

    XAML:

    <UserControl.Resources> 
        <mine:DateTimeConverter x:Key="MyDateTimeConverter" />
    </UserControl.Resources>    
    <Grid x:Name="LayoutRoot">
        <TextBox x:Name="myTextBox" Text="{Binding Path=TestDateTime, Converter={StaticResource MyDateTimeConverter}, Mode=TwoWay}" />
    </Grid>
    

    背后的C#代码:

     public partial class Page : UserControl
     {
          private TestClass m_testClass = new TestClass();
    
          public Page()
          {
               InitializeComponent();
               myTextBox.DataContext = m_testClass;
          }
      }
    

    使用属性设置器限制的C# TestClass:

    public class TestClass : INotifyPropertyChanged
    {
        private DateTime m_testDateTime = DateTime.Now;
        public DateTime TestDateTime
        {
            get { return m_testDateTime; }
            set
            {
                m_testDateTime = m_testDateTime.Date.Add(value.TimeOfDay);
                PropertyChanged(this, new PropertyChangedEventArgs("TestDateTime"));
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged = (t, e) => {};
    }
    

    C# IValueConverter:

    public class DateTimeConverter : IValueConverter
    {
        public object Convert(object value,
                           Type targetType,
                           object parameter,
                           CultureInfo culture)
        {
            DateTime date = (DateTime)value;
            return date.ToString("HH:mm");
        }
    
        public object ConvertBack(object value,
                                  Type targetType,
                                  object parameter,
                                  CultureInfo culture)
        {
            string strValue = value.ToString();
            DateTime resultDateTime;
            if (DateTime.TryParse(strValue, out resultDateTime))
            {
                return resultDateTime;
            }
            return value;
        }
    }
    

    【讨论】:

      【解决方案2】:

      我稍微扩展了 sipWiz 的解决方案,但我把它全部留在了我的模型类中

      我创建了两个字段 1.StartDay 2.StartTime,它们都在幕后访问了我的主要字段 StartDate

      public DateTime? StartTime
      {
          get
          {
              return StartDate;
          }
          set
          {
              if (StartDate == null) StartDate = DateTime.Today;
              StartDate = StartDate.Value.Date.Add(value.HasValue ? value.Value.TimeOfDay: TimeSpan.Zero);
          }
      }
      public DateTime? StartDay
      {
          get { return StartDate; }
          set
          {
              DateTime BaseDate = value.HasValue ? value.Value : DateTime.MinValue;
              StartDate = BaseDate.Date.Add(StartDate.HasValue ? StartDate.Value.TimeOfDay : TimeSpan.Zero);
          }
      }
      

      然后您可以将 DatePicker 绑定到日期字段,将 TextBox 绑定到 StringFormat="HH:mm" 的时间字段。

      【讨论】:

        猜你喜欢
        • 2021-01-22
        • 1970-01-01
        • 2012-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-16
        • 1970-01-01
        • 2019-07-05
        相关资源
        最近更新 更多