C# 如何通过基于验证的按钮命令将焦点设置为小数上下

C# 如何通过基于验证的按钮命令将焦点设置为小数上下,c#,wpf,xaml,.net-core,mvvm,C#,Wpf,Xaml,.net Core,Mvvm,XAML视图代码 <xctk:DecimalUpDown Value="{Binding InitialDepositAmount, Mode=TwoWay}" Minimum="{Binding MinimumAllowedAmount}" Maximum="{Binding MaximumAllowedAmount}" HorizontalAlignment="Right" Grid.Column=&quo

XAML视图代码

<xctk:DecimalUpDown Value="{Binding InitialDepositAmount, Mode=TwoWay}" Minimum="{Binding MinimumAllowedAmount}" Maximum="{Binding MaximumAllowedAmount}" HorizontalAlignment="Right" Grid.Column="1" VerticalAlignment="Center" Width="120" Height="21" Margin="5 0 0 0" />

<xctk:DecimalUpDown Value="{Binding CardPaymentAmount, Mode=TwoWay}" Minimum="{Binding MinimumAllowedAmount}" Maximum="{Binding MaximumAllowedAmount}" HorizontalAlignment="Right" Grid.Column="1" VerticalAlignment="Center" Width="120" Height="21" Margin="5 0 0 0" />

<Button Command="{Binding ProccessButtonCommand}" BorderBrush="#a7a7a7" BorderThickness="1" Grid.Column="0" HorizontalContentAlignment="Left" Width="162" Height="42" Margin="0 10 0 0">

视图模型代码


 private void ExecuteProccessButtonCommand(object obj)
 {
    if (InitialDepositAmount == 100)
    //Focus Initial Depostit DecimalUpDown
    if (CardPaymentAmount== 100)
    //Focus Card Payment DecimalUpDown
 }

 private DelegateCommand<object> _proccessButtonCommand;
 public DelegateCommand<object> ProccessButtonCommand =>
    _proccessButtonCommand ? ? (_proccessButtonCommand = new DelegateCommand<object> (ExecuteProccessButtonCommand));

私有void ExecuteProcessButtonCommand(对象obj)
{
如果(初始存款金额==100)
//聚焦初始Depostit小数向上向下
如果(CardPaymentAmount==100)
//Focus卡支付小数向上向下
}
私有DelegateCommand _进程按钮命令;
公共DelegateCommand过程按钮命令=>
_过程按钮命令?(_processbuttoncommand=newdelegatecommand(executeprocessbuttoncommand));

逻辑是这样的。我只想使用只使用MVVM的方法。

您可以为带有条件和目标按钮的
小数向上向下
创建附加行为。为此,您需要使用NuGet包

public class FocusConditionBehavior : Behavior<DecimalUpDown>
{
   public static readonly DependencyProperty TargetProperty = DependencyProperty.Register(
      nameof(Target), typeof(ButtonBase), typeof(FocusConditionBehavior), new PropertyMetadata(null, OnTargetChanged));

   public static readonly DependencyProperty ConditionProperty = DependencyProperty.Register(
      nameof(Condition), typeof(bool), typeof(FocusConditionBehavior), new PropertyMetadata(false));

   public ButtonBase Target
   {
      get => (ButtonBase)GetValue(TargetProperty);
      set => SetValue(TargetProperty, value);
   }

   public bool Condition
   {
      get => (bool)GetValue(ConditionProperty);
      set => SetValue(ConditionProperty, value);
   }

   private static void OnTargetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   {
      ((FocusConditionBehavior)d).OnTargetChanged((ButtonBase)e.OldValue, (ButtonBase)e.NewValue);
   }

   private void OnTargetChanged(ButtonBase oldButtonBase, ButtonBase newButtonBase)
   {
      if (oldButtonBase != null)
         oldButtonBase.Click -= OnClick;

      if (newButtonBase != null)
         newButtonBase.Click += OnClick;
   }

   private void OnClick(object sender, RoutedEventArgs e)
   {
      if (Condition)
         Keyboard.Focus(AssociatedObject);
   }
}
然后为应该触发评估条件和聚焦的按钮指定一个名称,例如
TestButton
,并将带有适当绑定的beahvior附加到
DecimalUpDown
s

<xctk:DecimalUpDown>
   <b:Interaction.Behaviors>
      <local:FocusConditionBehavior Condition="{Binding IsInitialDepositAmountMatching}"
                                    Target="{Binding ElementName=TestButton}"/>
   </b:Interaction.Behaviors>
</xctk:DecimalUpDown>
<xctk:DecimalUpDown>
   <b:Interaction.Behaviors>
      <local:FocusConditionBehavior Condition="{Binding IsCardPaymentAmountMatching}"
                                    Target="{Binding ElementName=TestButton}"/>
   </b:Interaction.Behaviors>
</xctk:DecimalUpDown>


通过这种方式,视图中的焦点处理与MVVM兼容,并打包为可重用组件作为奖励。

谢谢。我是否应该将FocusOnClickBehavior类添加到另一个文件中?@SamuelMarvinAguilos是的,应该将其放在单独的文件中。顺便说一句,我改为代码,所以它的拼写是
Properties
。确保全部更新。在这个场景中,我可以添加一个条件语句,因为元素不应该关注某些条件。谢谢你,萨缪尔马尔维纳基洛斯,你当然可以。您可以在
OnClick
中执行此操作,也可以创建其他依赖项属性来绑定条件的属性。更好的是,创建一个独立的依赖属性,如
IsEnabled
,然后将其绑定到XAML中。通过这种方式,您可以使用转换器甚至多值转换器来绑定属性,并且您的条件的逻辑不在焦点属性本身内,这也使得绑定更容易。抱歉,我有点困惑。您能告诉我如何在ViewModel部分处理它吗?而local:FocusOnClickProperties.Target不应该是local:FocusOnClickProperties.SetTarget吗?对不起,我是WPF的新手。
InitialDepositAmount
CardPaymentAmount
是否是独占的?如果两个条件均为
true
,则要将焦点设置到哪个控件?
<xctk:DecimalUpDown>
   <b:Interaction.Behaviors>
      <local:FocusConditionBehavior Condition="{Binding IsInitialDepositAmountMatching}"
                                    Target="{Binding ElementName=TestButton}"/>
   </b:Interaction.Behaviors>
</xctk:DecimalUpDown>
<xctk:DecimalUpDown>
   <b:Interaction.Behaviors>
      <local:FocusConditionBehavior Condition="{Binding IsCardPaymentAmountMatching}"
                                    Target="{Binding ElementName=TestButton}"/>
   </b:Interaction.Behaviors>
</xctk:DecimalUpDown>