首页 > 解决方案 > RadNumericUpDown 增量率

问题描述

RadNumericUpDown在 C# Prism MVVM 应用程序中使用 Telerik 控件。当我的用户按住向上或向下箭头时,控件会增加它所绑定的属性的值。但是,当该属性值更改时,会通过通信线路将命令发送到另一个设备,但会有一些延迟。此外,在某些情况下,设备无法成功更改此参数的值。

这是我认为的代码。

<telerik:RadNumericUpDown x:Name="VoltageNumericControl" HorizontalAlignment="Left" Margin="165,0,0,0" Grid.RowSpan="1" VerticalAlignment="Center" Grid.Row="2" NumberDecimalDigits="2" Maximum="10.00" Minimum="0.00" SmallChange="0.01" ValueFormat="Numeric"  
Value="{Binding ModelDevice.Voltage, Mode=TwoWay, Delay= 50}"/>

它增加了我的 ViewModel 中的值,例如:

 public double Voltage
 {
     get { return voltage; }
     set
     {
         dataService.SetVoltage(value);   // Triggers command to external device
         SetProperty(ref voltage, value); // Updates local value temporarily
         // Value is then updated an indeterminate period later when a response is 
         // received from the external device 
     }
 }

如何更改RadNumericUpDown控件的行为,以便按住按钮或箭头键不会尝试不断更新其绑定到的属性的值?

标签: c#wpfxamltelerikprism

解决方案


如何在外部设备请求未决时禁用控件并再次启用它,当您有响应并且知道正确的值时?派生自的每个控件UIElement都有一个属性IsEnabled,您可以使用它来停用用户输入(控件显示为灰色)。

获取或设置一个值,该值指示此元素是否在用户界面 (UI) 中启用 [...] 未启用的元素不参与命中测试或焦点,因此不会成为输入事件的来源

您可以在视图模型中创建属性IsExternalDeviceNotPending。并将其绑定IsEnabledRadNumericUpDown. 注意..NotPending(启用意味着没有外部设备挂起)。如果你想让它成为一个肯定的条件 ( ...Pending),你必须在 XAML 中使用一个值转换器来否定这个值,所以这更简单。

private bool isExternalDeviceNotPending;
public bool IsExternalDeviceNotPending
{
    get => isExternalDeviceNotPending;
    private set => SetProperty(ref isExternalDeviceNotPending, value);
    }
}
<telerik:RadNumericUpDown x:Name="VoltageNumericControl" IsEnabled="{Binding IsExternalDeviceNotPending}" ... />

现在您可以将 设置为何时IsExternalDeviceNotPending设置,这反过来又开始与外部设备的通信。然后该控件将被禁用。falseVoltage

public double Voltage
{
    get { return voltage; }
    set
    {
        IsExternalDeviceNotPending = false;
        dataService.SetVoltage(value);
        SetProperty(ref voltage, value);
    }
}

通信完成后,将其重置为true, 以再次启用控件并允许用户输入。


推荐阅读