输入文本时 DatePicker.SelectedDate 不改变

发布于 2024-10-09 12:34:56 字数 515 浏览 4 评论 0原文

当我的用户通过 DatePicker 中的 Calander 控件选择日期时,该值会正确绑定到基础对象。但是,如果用户在 DatePicker 中键入日期,然后单击按钮,则文本不会设置为 SelectedDate 属性。

用户必须将光标从 DatePicker 内的 TextBox 中移除才能更新绑定对象。

 <toolkit:DatePicker Name="_dpField" Grid.Column="1" MinWidth="100"
               ToolTip="{Binding Path=ToolTipText}"
               TextInput="_dpField_TextInput"
               SelectedDate="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>

帮助!我如何确保在按钮事件代码中使用此键入的值?

谢谢!

When my users select a date via the Calander control within the DatePicker, the value gets correctly bound to the underlying object. BUT, if the user types the date within the DatePicker, then clicks a button, the text is not set to the SelectedDate property.

The user has to remove the cursor from the TextBox within the DatePicker for the bound object to be updated.

 <toolkit:DatePicker Name="_dpField" Grid.Column="1" MinWidth="100"
               ToolTip="{Binding Path=ToolTipText}"
               TextInput="_dpField_TextInput"
               SelectedDate="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>

HELP! how do i make sure that this typed value is used within the buttons event code?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

维持三分热 2024-10-16 12:34:56

我找到了一个更简单的解决方案,不需要 DateConverter

我仅绑定到 Text 属性并使用 TargetNullValue=''

<DatePicker x:Name = "dpDisbursementDate" 
            Text = "{Binding NameOfMyProperty, Mode=TwoWay,    
            UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, 
            TargetNullValue=''}"/>

I found a easier solution where I don't need the DateConverter.

I only bound to the Text Property and use TargetNullValue=''.

<DatePicker x:Name = "dpDisbursementDate" 
            Text = "{Binding NameOfMyProperty, Mode=TwoWay,    
            UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, 
            TargetNullValue=''}"/>
真心难拥有 2024-10-16 12:34:56

您可以使用转换器将键入的文本解析为有效的日期时间

示例

 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string strValue = System.Convert.ToString(value);
        DateTime resultDateTime;
        if (DateTime.TryParse(strValue, out resultDateTime))
        {
            return resultDateTime;
        }
        return value;

    }

Xaml

     <Controls:DatePicker 
     Text="{Binding OrderDate,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
     SelectedDate="{Binding RelativeSource={RelativeSource Self},Path=Text,
     Converter={StaticResource DateConverter}}">

You can use a converter for parsing your typed text to a valid datetime

Sample

 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string strValue = System.Convert.ToString(value);
        DateTime resultDateTime;
        if (DateTime.TryParse(strValue, out resultDateTime))
        {
            return resultDateTime;
        }
        return value;

    }

Xaml

     <Controls:DatePicker 
     Text="{Binding OrderDate,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
     SelectedDate="{Binding RelativeSource={RelativeSource Self},Path=Text,
     Converter={StaticResource DateConverter}}">
南笙 2024-10-16 12:34:56

这是一个简单的解决方案,即使使用欧洲/德国日期格式“dd.MM.yyyy”也能帮助我。

添加到您的 xaml 根元素

xml:lang="de-AT"

,日期选择器如下所示:

<DatePicker SelectedDate="{Binding PropertyName, StringFormat=dd.MM.yyyy}" Name="datePicker" />

希望它适合您!

Here a simple solution, that helped me even with the european/german date format "dd.MM.yyyy".

Add to your xaml root element

xml:lang="de-AT"

and the datepicker looks like this:

<DatePicker SelectedDate="{Binding PropertyName, StringFormat=dd.MM.yyyy}" Name="datePicker" />

hope it works for you!

ゝ偶尔ゞ 2024-10-16 12:34:56

我能找到的唯一解决方案是通过设置 Focusable="False" 来禁止在 DatePicker 中输入日期,并且只允许从日历中进行选择。这样我们至少可以确保获得正确的日期。

The only solution that I could find is to disable entering of the date in the DatePicker by setting Focusable="False" and only allowing selection from the calendar. This way we can at least make sure that we get the correct date.

江湖正好 2024-10-16 12:34:56

在我的特定情况下,我有一个按钮,在从文本更新日期之前在其 ViewModel 中启动了一个操作。但是,将下面的函数绑定到“单击”按钮(“单击”和“操作”均已绑定)会覆盖操作开始之前所需的日期。

private void UpdateDateBeforeAction(object sender, RoutedEventArgs e)
{
    bool parseSuccess = DateTime.TryParse(this.myDatePicker.Text, out DateTime parsedDate);
    if (parseSuccess && this.DataContext is MyParticularViewModel vm)
    {
        vm.targetDate = parsedDate;
    }
}

编辑:实际上你甚至不需要触摸视图模型

if(parseSuccess)
{
    this.myDatePicker.SelectedDate = parsedDate;
}

In my particular case I had a Button that started an action in its ViewModel before the date was updated from text. However binding the function below to the buttons Click (both Click and Action are bound) overwrites the required date before the action starts.

private void UpdateDateBeforeAction(object sender, RoutedEventArgs e)
{
    bool parseSuccess = DateTime.TryParse(this.myDatePicker.Text, out DateTime parsedDate);
    if (parseSuccess && this.DataContext is MyParticularViewModel vm)
    {
        vm.targetDate = parsedDate;
    }
}

Edit: Actually you don't even need to touch the viewmodel

if(parseSuccess)
{
    this.myDatePicker.SelectedDate = parsedDate;
}
Smile简单爱 2024-10-16 12:34:56

这可能有点晚了,但我已经坚持了一段时间了。

如果您有另一个 WPF 元素,您可以将焦点更改为按钮按下事件开始时的元素,这将使日期选择器处理在其文本框中输入的任何文本。我只用组合框尝试过此操作,但它似乎有效,并且它允许您仍然对日期进行自定义格式(即 26/04/2016 而不是 04/26/2016)。我假设如果您没有任何可以将焦点更改为的内容,您也可以使用不可见元素。

    private void btnInbound_Complete_Click(object sender, RoutedEventArgs e)
    {
        if (Validation())
        {
            comboInbound_Result.Focus();//THIS IS SO THAT ANY MANUAL DATEPICKER ENTRY IS ACCEPTED BEFORE THE REST OF THE BUTTON CODE IS RUN
            SQLinbound_CompleteItem();
            ClearAll();
        }
    }

This is probably a bit late, but I've been stuck on this for a while now.

If you have another WPF element you can change focus to that at the beginning of your button press event, this will make the datepicker process any text entered in it's textbox. I've only tried this with a combobox but it seems to work and it allows you to still have custom formatting on your dates (ie 26/04/2016 rather than 04/26/2016). I assume you would be able to use an invisible element as well if you don't have anything to change focus to.

    private void btnInbound_Complete_Click(object sender, RoutedEventArgs e)
    {
        if (Validation())
        {
            comboInbound_Result.Focus();//THIS IS SO THAT ANY MANUAL DATEPICKER ENTRY IS ACCEPTED BEFORE THE REST OF THE BUTTON CODE IS RUN
            SQLinbound_CompleteItem();
            ClearAll();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文