简体   繁体   English

MVVM中的WPF扩展工具包DateTimePicker更改值事件

[英]WPF Extended Toolkit DateTimePicker change value event in MVVM

i have a question. 我有个问题。

I am using 2 Extended Toolkit DateTimePicker, and mu goal is situation where if you change time one start picker end picker will update with time 5 minutes later, and vice versa if i change value on end start will go back 5 minutes. 我正在使用2 Extended Toolkit DateTimePicker,mu目标是如果您更改时间,则一个开始选择器结束选择器将在5分钟后随时间更新,反之亦然,如果我更改结束开始时的值将返回5分钟。

I tried with ValueChanged property but no luck it end up with an exception like: 我尝试使用ValueChanged属性,但不幸的是,它最终出现如下异常:

The operation of entering the value of the "System.Windows.Data.Binding" element triggered an exception. 输入“ System.Windows.Data.Binding”元素的值的操作触发了异常。

Xaml for those controls is simple: 这些控件的Xaml很简单:

<xctk:DateTimePicker Value="{Binding StartEventDatetime}" ValueChanged="{Binding UpdateEndDatetime}"
                                             Format="Custom" FormatString="yyyy/MM/dd HH:mm:ss" Watermark="Start Date" Foreground="White"/>
<xctk:DateTimePicker Value="{Binding EndEventDatetime}" ValueChanged="{Binding UpdateStartDatetime}"
                                             Format="Custom" FormatString="yyyy/MM/dd HH:mm:ss" Watermark="End date" Foreground="White"/>

And command binding: 和命令绑定:

public ICommand UpdateStartDatetime => new Command(p => { EndEventDatetime = EndEventDatetime.AddMinutes(CurrentSamplesViewModel.MinuteTimeInterval); });
public ICommand UpdateEndDatetime => new Command(p => { StartEventDatetime = StartEventDatetime.AddMinutes(CurrentSamplesViewModel.MinuteTimeInterval); });

Is there a way to create such an event?? 有没有办法创建这样的事件?

Take a look at interraction triggers . 看一下触发触发器 with them you can call a command in your vm with a specific event from your ui 与他们一起,您可以通过ui中的特定事件在vm中调用命令

Ive made a solution based on Interaction.Behaviors xaml would look like: Ive根据Interaction提出了一个解决方案。行为xaml如下所示:

<xctk:DateTimePicker Value="{Binding StartEventDatetime}" Format="Custom" FormatString="yyyy/MM/dd HH:mm" Watermark="Start Date" Foreground="White">

<i:Interaction.Behaviors>
  <businessLogic:PropertyChangeBehavior DependencyPropertyName="Value" PropertyChangedCommand="{Binding UpdateEndDatetimeCommand}"/>
</i:Interaction.Behaviors>

</xctk:DateTimePicker>

   <xctk:DateTimePicker Value="{Binding EndEventDatetime}" Format="Custom" FormatString="yyyy/MM/dd HH:mm" Watermark="End date" Foreground="White">

         <i:Interaction.Behaviors>
              <businessLogic:PropertyChangeBehavior DependencyPropertyName="Value" PropertyChangedCommand="{Binding UpdateStartDatetimeCommand}"/>
          </i:Interaction.Behaviors>

     </xctk:DateTimePicker>

Logic for PropertyChangedCommand: PropertyChangedCommand的逻辑:

public class PropertyChangeBehavior : Behavior { public static readonly DependencyProperty PropertyChangedCommandProperty = DependencyProperty.Register( "PropertyChangedCommand", typeof(ICommand), typeof(PropertyChangeBehavior), new PropertyMetadata(null, ApplyChanged)); 公共类PropertyChangeBehavior:行为{公共静态只读DependencyProperty PropertyChangedCommandProperty = DependencyProperty.Register(“ PropertyChangedCommand”,typeof(ICommand),typeof(PropertyChangeBehavior),new PropertyMetadata(null,ApplyChanged));

    public static readonly DependencyProperty DependencyPropertyNameProperty = DependencyProperty.Register(
        "DependencyPropertyName", typeof(string),
        typeof(PropertyChangeBehavior), new PropertyMetadata(string.Empty, ApplyChanged));

    private DependencyPropertyDescriptor descriptor;

    public ICommand PropertyChangedCommand
    {
        get => (ICommand)GetValue(PropertyChangedCommandProperty);
        set => SetValue(PropertyChangedCommandProperty, value);
    }

    public string DependencyPropertyName
    {
        get => (string)GetValue(DependencyPropertyNameProperty);
        set => SetValue(DependencyPropertyNameProperty, value);
    }

    private static void ApplyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is PropertyChangeBehavior behavior)
        {
            behavior.Setup();
        }
    }

    protected override void OnAttached()
    {
        Setup();
    }

    protected override void OnDetaching()
    {
        if (descriptor != null && AssociatedObject != null)
        {
            descriptor.RemoveValueChanged(AssociatedObject, OnPropertyValueChanged);
        }
    }

    private void Setup()
    {
        if (descriptor != null || string.IsNullOrEmpty(DependencyPropertyName) || AssociatedObject == null)
        {
            return;
        }

        descriptor = DependencyPropertyDescriptor.FromName(DependencyPropertyName, AssociatedObject.GetType(),
            AssociatedObject.GetType());

        descriptor?.AddValueChanged(AssociatedObject, OnPropertyValueChanged);
    }

    private void OnPropertyValueChanged(object sender, EventArgs e)
    {
        object value = AssociatedObject.GetValue(descriptor.DependencyProperty);
        if (PropertyChangedCommand == null || !PropertyChangedCommand.CanExecute(value))
        {
            return;
        }

        PropertyChangedCommand.Execute(value);
    }
}

And commands remain the same, and it works. 并且命令保持不变,并且可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM