[英]C# WPF .Net4.8 Framework ReadOnly TextBox Text Binding Delayed Update
I have a string "ConnectionStatus" which is bound to a textbox text property
我有一个绑定到文本框文本属性的字符串“ConnectionStatus”
private string connectionStatus;
public string ConnectionStatus
{
get
{
return connectionStatus;
}
set
{
if (connectionStatus != value)
{
connectionStatus = value;
NotifyPropertyChanged("ConnectionStatus");
}
}
}
I have a button connected to a RelayCommand:
我有一个连接到 RelayCommand 的按钮:
void ConnectCmd(object parameter)
{
ConnectionStatus = "Connecting..";
// Do some stuff
ConnectionStatus = "Connected"
}
XML
XML
<TextBox x:Name="TextBlock_ConectedToName"
Grid.Column="1"
Grid.Row="0"
Grid.ColumnSpan="2"
Height="40"
Margin="5"
FontSize="26"
IsReadOnly="True"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Text="{Binding ConnectionStatus,
UpdateSourceTrigger=PropertyChanged}" />
When the button is pressed, the Relay Command is called, which then updates the string "ConnectionStaus" to "Connecting...", I then do some processing, opening a serial port, and change ConnectionStatus to "Connected" if successful.
当按下按钮时,调用中继命令,然后将字符串“ConnectionStaus”更新为“正在连接...”,然后我进行一些处理,打开一个串口,如果成功则将 ConnectionStatus 更改为“已连接”。 However, the "Connecting..." text is never displayed, The text updating only seems to trigger when the RelayCommand function finishes.
但是,永远不会显示“正在连接...”文本,文本更新似乎仅在 RelayCommand function 完成时触发。 How do I get the text to update immediately.
如何让文本立即更新。
You're running your code in that relaycommand on the default ui thread.您正在默认 ui 线程上的该 relaycommand 中运行您的代码。 The UI updates on the UI thread.
UI 在 UI 线程上更新。 It can only do one thing at once.
它一次只能做一件事。
You could make your relaycommand async and free up the ui thread momentarily.您可以使您的中继命令异步并暂时释放 ui 线程。 using.
使用。
await Task.Delay(20);
Don't forget to put async in your relaycommand or use an implementation does that for you.不要忘记将 async 放入您的中继命令或使用实现为您执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.