0

我有 3 个文本框来编辑 DateTime。需要注意的是,这 2 个 TextBox 正在编辑第一个 TextBox DateTime 值的小时和分钟。1 编辑日期,2 编辑小时和分钟。你会怎么做?下面的代码在编辑小时或分钟时不反映 DateTime 更改,因为它会执行 ToString("HH") 并且 DateTime 值丢失:

 <TextBox Text="{Binding MyDateTime}"  />

    <!--This cannot work : it's just for clearing purposes -->
    <TextBox Text="{Binding MyDateTime, StringFormat=\{0:HH\}}}"  />
    <TextBox Text="{Binding MyDateTime}, StringFormat=\{0:mm\}}"  />

当然,我可以有一个 ViewModel 作为 DataContext,我可以在其中以编程方式解决它。但我只想知道 XAML 中是否有直接的可能性

4

4 回答 4

2

仅使用 XAML 并不容易。如何解决这个问题有几种可能性:

1.编写自定义控件或用户控件可以做到这一点

您可以编写一个自定义控件/用户控件(例如 DateTimeTextBox),该控件具有DateTime Value您的 xaml 可以绑定的属性,并且包含转换为在其两个文本框之一中输入的日期时间值的逻辑。除了两个文本框,您还可以使用 maskedtextbox 之类的东西。

2. ViewModel 中的两个专用属性

如果您使用 MVVM,您可以为您的 ViewModel 提供两个专用属性int DateTimeHours int DateTimeMinutes并与之绑定:

<TextBox Text="{Binding MyDateTimeHours}"  />
<TextBox Text="{Binding MyDateTimeMinutes}"  />

然后,您的 ViewModel 会将这两个属性合并为一个 DateTime 值。

于 2010-05-21T08:09:37.793 回答
0

您将需要使用具有 2way 绑定和转换器参数的转换器。将原始小时保存在转换器中。然后您可以适当地更新绑定源。

于 2010-05-21T07:58:05.910 回答
0

我很欣赏这是一篇旧帖子,但我今天遇到了这个并找到了另一种处理日期和时间绑定的方法。

我创建了一个名为 dateTime 的部分类,它有 4 个属性,一个用于日期,一个用于小时,另一个用于分钟。然后是一个只读的 dateTime 属性,它返回一个完成的日期。

    Partial Public Class dateTimeValue
        Property Dt As Date
        Property Hr As Integer
        Property Mn As Integer

        ReadOnly Property dateTime As Date
            Get
                Dim d = Dt
                d = d.AddHours(d.Hour * -1).AddHours(Hr)
                d = d.AddMinutes(d.Minute * -1).AddMinutes(Mn)
                d = d.AddSeconds(d.Second * -1)
                Return d
            End Get
        End Property
    End Class

然后在 XAML 中,我使用了一个网格布局,并绑定了一个 DateTimePicker 和几个 ComboBox。

            <UniformGrid Columns="2">
                <TextBlock Text="Date"/>
                <DatePicker SelectedDate="{Binding dateTime.Dt}"/>
                <TextBlock Text="Time"/>
                <WrapPanel>
                    <ComboBox SelectedValue="{Binding dateTime.Hr}" SelectedValuePath="Content">
                        <ComboBoxItem>00</ComboBoxItem>
                        <ComboBoxItem>01</ComboBoxItem>
                        <ComboBoxItem>02</ComboBoxItem>
                        .........
                        <ComboBoxItem>22</ComboBoxItem>
                        <ComboBoxItem>23</ComboBoxItem>
                    </ComboBox>
                    <TextBlock Text=":"/>
                    <ComboBox SelectedValue="{Binding dateTime.Mn}" SelectedValuePath="Content">
                        <ComboBoxItem>00</ComboBoxItem>
                        <ComboBoxItem>15</ComboBoxItem>
                        <ComboBoxItem>30</ComboBoxItem>
                        <ComboBoxItem>45</ComboBoxItem>
                    </ComboBox>
                </WrapPanel>
                <Button Click="saveTask" Content="Save Task"/>
            </UniformGrid>

然后在你可以使用的文本块中显示正确的日期和时间

<TextBlock Text="{Binding dateTime.dateTime, StringFormat={}{0:dd MMM yyyy - HH:mm}}"/>

希望这可以帮助其他人。

于 2015-10-07T10:22:04.757 回答
0

100% MVVM

public class DateTimeConverter : IValueConverter
{
    private DateTime _target = DateTime.Now;
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var source = value as DateTime?;
        if (source is null) return null;
        _target = source.Value;
        switch (parameter as string)
        {
            case "y": return source.Value.Year;
            case "m": return source.Value.Month;
            case "d": return source.Value.Day;
            case "h": return source.Value.Hour;
            case "i": return source.Value.Minute;
            default: return null;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        switch (parameter as string)
        {
            case "y": return new DateTime(System.Convert.ToInt32(value), _target.Month, _target.Day, _target.Hour, _target.Minute, 0);
            case "m": return new DateTime(_target.Year, System.Convert.ToInt32(value), _target.Day, _target.Hour, _target.Minute, 0);
            case "d": return new DateTime(_target.Year, _target.Month, System.Convert.ToInt32(value), _target.Hour, _target.Minute, 0);
            case "h": return new DateTime(_target.Year, _target.Month, _target.Day, System.Convert.ToInt32(value), _target.Minute, 0);
            case "i": return new DateTime(_target.Year, _target.Month, _target.Day, _target.Hour, System.Convert.ToInt32(value), 0);
            default: return _target;
        }
    }
}
于 2020-04-09T12:36:33.120 回答