C# 将 DateTime 绑定到日期和时间 EditFields
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/708843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bind DateTime to Date and Time EditFields
提问by
i am trying to build a gui that includes editing a DateTime value of an object. The DateTime Property has binding to a DataPicker and a normal TextBox for the Time. When i change the value in the Time TextBox the value wirtten in the DateTime Property is Today with the entered Time instead of just updating the Time, preserving the original Date.
我正在尝试构建一个 gui,其中包括编辑对象的 DateTime 值。DateTime 属性绑定到一个 DataPicker 和一个普通的 Time 文本框。当我更改时间文本框中的值时,DateTime 属性中的值是今天,输入的时间而不是仅更新时间,保留原始日期。
How can i implement a Time TextBox that only changes the DateTime time but not the date?
如何实现只更改 DateTime 时间而不更改日期的 Time TextBox?
Current Binding:
当前绑定:
<TextBlock>Time</TextBlock>
<TextBox Template="{StaticResource RoundedTextBoxTemplate}">
<Binding Path="Delivery.Date" StringFormat="HH:mm">
<Binding.ValidationRules>
<v:IsValidTimeRule />
</Binding.ValidationRules>
</Binding>
</TextBox>
回答by sipwiz
The only way I could think of to do it is to have a DateTime property where you only allow the setter change the time.
我能想到的唯一方法是拥有一个 DateTime 属性,您只允许设置器更改时间。
XAML:
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# Code behind:
背后的 C# 代码:
public partial class Page : UserControl
{
private TestClass m_testClass = new TestClass();
public Page()
{
InitializeComponent();
myTextBox.DataContext = m_testClass;
}
}
C# TestClass where the property setter restriction is used:
使用属性设置器限制的 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:
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;
}
}
回答by Rabbi
I extended sipWiz's solution a bit but I left it all in my Model class
我稍微扩展了 sipWiz 的解决方案,但我把它全部留在了我的模型类中
I created two fields 1. StartDay
2. StartTime
which both access my primary field StartDate
behid the scenes.
我创建了两个字段 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);
}
}
Then you can Bind the DatePicker to the day field and the TextBox to the time field with StringFormat="HH:mm".
然后,您可以使用 StringFormat="HH:mm" 将 DatePicker 绑定到日期字段,将 TextBox 绑定到时间字段。