c# - 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
控件的行为,以便按住按钮或箭头键不会尝试不断更新其绑定到的属性的值?
解决方案
如何在外部设备请求未决时禁用控件并再次启用它,当您有响应并且知道正确的值时?派生自的每个控件UIElement
都有一个属性IsEnabled
,您可以使用它来停用用户输入(控件显示为灰色)。
获取或设置一个值,该值指示此元素是否在用户界面 (UI) 中启用 [...] 未启用的元素不参与命中测试或焦点,因此不会成为输入事件的来源。
您可以在视图模型中创建属性IsExternalDeviceNotPending
。并将其绑定IsEnabled
到RadNumericUpDown
. 注意..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
设置,这反过来又开始与外部设备的通信。然后该控件将被禁用。false
Voltage
public double Voltage
{
get { return voltage; }
set
{
IsExternalDeviceNotPending = false;
dataService.SetVoltage(value);
SetProperty(ref voltage, value);
}
}
通信完成后,将其重置为true
, 以再次启用控件并允许用户输入。
推荐阅读
- python - Django 表单。查看带有登录用户模型的表单
- android - 无法获得完整的活动列表
- java - 如何在使用 java 的到期时间之前使 JWT 令牌无效?
- java - 如果其他,我错过了什么?
- java - 放大或缩小文本以适应标签容器 JavaFX
- c - 当使用 UDP 进行文件传输(套接字 API)时,检测何时停止从服务器接收数据( recfrom() )
- python - Astral 封装和矢量化
- flutter - 有没有任何flutter升级/更新没有问题?
- typeorm - TypeORM:QueryFailedError:关系不存在
- python - 如何仅为公会制作可切换命令(Discord.py)