我有這個像魅力一樣作業的 XAML 代碼:
<TextBox Text="{Binding MyTextProperty, UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
<KeyBinding Command="{Binding MyOwnCommand}" CommandParameter="{Binding MyTextProperty}" Key="Enter" />
</TextBox.InputBindings>
</TextBox>
在那里,我有MyTextProperty那個作為引數傳遞給MyOwnCommand我按回車鍵。
我不希望MyTextProperty每次輸入字母時都更新(因為它有一些相關的邏輯),但我確實希望它在我完成輸入后執行(不按回車鍵或失去焦點)。理想的解決方案是:
<TextBox Text="{Binding MyTextProperty, UpdateSourceTrigger=PropertyChanged, Delay=400}">
<TextBox.InputBindings>
<KeyBinding Command="{Binding MyOwnCommand}" CommandParameter="{Binding MyTextProperty}" Key="Enter" />
</TextBox.InputBindings>
</TextBox>
這里的重點是"Delay=400"引數。它等到我完成輸入然后更新MyTextProperty。
但此時我發現的問題是,如果我輸入一些內容并立即按回車,MyOwnCommand會呼叫但MyTextProperty尚未更新(它將在 400 毫秒后更新)。
我試圖在 中添加相同的延遲CommandParameter="{Binding MyTextProperty, Delay=400}",但它不起作用。
MyTextProperty更新后傳遞 CommandParameter 的正確方法是什么?
uj5u.com熱心網友回復:
TextBox.Text 在用戶從鍵盤鍵入符號后立即更改,即使將值發送到系結屬性存在延遲。因此,您可以直接將 CommandParameter 系結到 TextBox.Text:
<TextBox Name="MyTextBox"
Text="{Binding MyTextProperty, UpdateSourceTrigger=PropertyChanged, Delay=400}">
<TextBox.InputBindings>
<KeyBinding Command="{Binding MyOwnCommand}"
CommandParameter="{Binding Text, ElementName=MyTextBox}"
Key="Enter" />
</TextBox.InputBindings>
</TextBox>
uj5u.com熱心網友回復:
但我確實希望它在我完成輸入后執行
我會將這個屬性拆分為不同的屬性。然后只需輸入命令提取最終值,在最終屬性中設定并執行最后一步。
// bound to the active textbox, which receives character by character changes
public string MyTextProperty { get { ... }
set { ...do individual key press logic here... }
public string MyTextProperty_Final { }
public void EnterCommand()
{
MyTextProperty_Final = MyTextProperty;
FinalOperationCommand(MyTextProperty_Final); // Or FinalOperationCommand.Invoke(MyTextProperty_Final);
}
public void FinalOperationCommand(string text)
{
... delay if needed ...
... Do stuff with MyTextProperty_Final
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/404806.html
標籤:
上一篇:WPF:如何在TextBlock中使用整數(來自C#-Code)?
下一篇:云母材料未顯示在輔助視窗上
