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.
I tried with ValueChanged property but no luck it end up with an exception like:
The operation of entering the value of the "System.Windows.Data.Binding" element triggered an exception.
Xaml for those controls is simple:
<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
Ive made a solution based on Interaction.Behaviors xaml would look like:
<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:
public class PropertyChangeBehavior : Behavior { public static readonly 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.