MVVM 中继命令在 Silverlight NumericUpDown 更改值之前触发

发布于 2024-10-05 02:35:42 字数 898 浏览 0 评论 0原文

如果您有绑定到 MVVM 属性和 RelayCommand 触发器集(任何事件)的 Silverlight 工具包 NumericUpDown 控件,则在 NumericUpDown 更改 MVVM 属性值之前调用该命令。这意味着,您不能在方法/操作/命令中使用新的(更改的)值...

XAML:

<inputToolkit:NumericUpDown x:Name="testNum" Value="{Binding RegisterForm, Mode=TwoWay}">
<i:Interaction.Triggers>
  <i:EventTrigger EventName="ValueChanged">
    <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding DoSomethingCommand}"/>
  </i:EventTrigger>
</i:Interaction.Triggers>
</inputToolkit:NumericUpDown>

MVVM (C#):

DoSomethingCommand = new RelayCommand(() =>
            {
                OtherRegisterForm = RegisterForm;
            });

在这种情况下,如果您的 v 值为 0 并且在 NumericUpDown 控件中输入新值 123它会在 MVVM 属性上的“RaisePropertyChange”事件之前触发“DoSomethingCommand”。 “OtherRegisterForm”将是 0 而不是 123。

有没有办法让它工作?

If you have a Silverlight Toolkit NumericUpDown control binded to a MVVM property and a RelayCommand trigger set (any event), the command is called before NumericUpDown changes MVVM property value. This means, you can not use the new (changed) value with you method/action/command...

XAML:

<inputToolkit:NumericUpDown x:Name="testNum" Value="{Binding RegisterForm, Mode=TwoWay}">
<i:Interaction.Triggers>
  <i:EventTrigger EventName="ValueChanged">
    <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding DoSomethingCommand}"/>
  </i:EventTrigger>
</i:Interaction.Triggers>
</inputToolkit:NumericUpDown>

MVVM (C#):

DoSomethingCommand = new RelayCommand(() =>
            {
                OtherRegisterForm = RegisterForm;
            });

In this case, if you have v value 0 and you input a new value 123 in NumericUpDown control it triggers the "DoSomethingCommand" before "RaisePropertyChange" event on MVVM property.
"OtherRegisterForm" would be 0 and not 123.

Is there a way to make this work?

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

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

发布评论

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

评论(1

烟酉 2024-10-12 02:35:43

哦,天哪,这并不容易,但你在这里:

xaml 部分:

<toolkit:NumericUpDown Value="{Binding SomeNumber}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="ValueChanged">
                <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding MyCommand}" PassEventArgsToCommand="True" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </toolkit:NumericUpDown>

和 cs 代码:

    public class MainViewModel : ViewModelBase
{

    public double SomeNumber { get; set; }

    public MainViewModel()
    {
        SomeNumber = 10;
        MyCommand = new RelayCommand<RoutedPropertyChangedEventArgs<double>>(myActionMethod);
    }

    public RelayCommand<RoutedPropertyChangedEventArgs<double>> MyCommand { get; set; }

    public void myActionMethod(RoutedPropertyChangedEventArgs<double> arg)
    {
        MessageBox.Show(arg.NewValue.ToString());
    }
}

希望有帮助,Arek

oh boy, wasn't easy but here u are :

xaml part :

<toolkit:NumericUpDown Value="{Binding SomeNumber}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="ValueChanged">
                <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding MyCommand}" PassEventArgsToCommand="True" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </toolkit:NumericUpDown>

and cs code :

    public class MainViewModel : ViewModelBase
{

    public double SomeNumber { get; set; }

    public MainViewModel()
    {
        SomeNumber = 10;
        MyCommand = new RelayCommand<RoutedPropertyChangedEventArgs<double>>(myActionMethod);
    }

    public RelayCommand<RoutedPropertyChangedEventArgs<double>> MyCommand { get; set; }

    public void myActionMethod(RoutedPropertyChangedEventArgs<double> arg)
    {
        MessageBox.Show(arg.NewValue.ToString());
    }
}

hope that helps, Arek

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文