我正在使用2扩展工具包DateTimePicker,并且mu目标是这样的情况:如果你改变时间,一个开始选择器结束选择器将在5分钟后随时间更新,反之亦然如果我在结束开始时改变值将返回5分钟。
我尝试使用ValueChanged属性,但没有运气,最终导致例外情况:
输入“System.Windows.Data.Binding”元素的值的操作触发了异常。
这些控件的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"/>
命令绑定:
public ICommand UpdateStartDatetime => new Command(p => { EndEventDatetime = EndEventDatetime.AddMinutes(CurrentSamplesViewModel.MinuteTimeInterval); });
public ICommand UpdateEndDatetime => new Command(p => { StartEventDatetime = StartEventDatetime.AddMinutes(CurrentSamplesViewModel.MinuteTimeInterval); });
有没有办法创建这样的事件?
答案 0 :(得分:0)
看看interraction triggers。使用它们,您可以使用您的ui中的特定事件在您的虚拟机中调用命令
答案 1 :(得分:0)
我做了一个基于Interaction.Behaviors xaml的解决方案看起来像:
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);
}
}
PropertyChangedCommand的逻辑:
public class PropertyChangeBehavior:Behavior { public static readonly DependencyProperty PropertyChangedCommandProperty = DependencyProperty.Register( &#34; PropertyChangedCommand&#34;,typeof(ICommand), typeof(PropertyChangeBehavior),new PropertyMetadata(null,ApplyChanged));
struct
命令保持不变,并且有效。