簡體   English   中英

如何在WPF TextBox上強制TextChanged事件並保留焦點?

[英]How to force a TextChanged event on a WPF TextBox and mantain the focus?

我在WPF中創建一個數字TextBox,有兩個按鈕來增加和減少值。

我創建了兩個RoutedCommand來管理behia,它們運行良好。 我只想解決一個問題。 我希望控件在執行增加或減少命令時通知綁定到其TextProperty的所有對象。

目前, 只有當我將焦點更改為另一個控件時,它才會發送通知

非常感謝任何幫助,謝謝

有一個簡單的方法:

Text="{Binding Path=MyProperty, UpdateSourceTrigger=PropertyChanged}"

(我在TextBox上測試過它)。 祝好運

在Binding和TextChanged事件中使用UpdateSourceTrigger="Explicit"更新BindingSource 所以你寫的是這樣的:

<NumericTextBox x:Name="control" Text={Binding Path=MyProperty}/>

而是這樣做

<NumericTextBox x:Name="control" Text={Binding Path=MyProperty, UpdateSourceTrigger=Explicit}/>

並在TextChanged事件處理程序中更新綁定。

control.GetBindingExpression(NumericTextBox.TextProperty).UpdateSource();

那就完成了。 希望能幫助到你!!

TextBox上綁定Text屬性的默認行為是在LostFocus上更新。 您可以通過覆蓋靜態ctor中TextProperty上的元數據來更改自定義控件中的此值:

static NumericTextBox()
{
    TextProperty.OverrideMetadata(
        typeof(NumericTextBox),
        new FrameworkPropertyMetadata("", // default value
            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
            null,  // property change callback
            null,  // coercion callback
            true,  // prohibits animation
            UpdateSourceTrigger.PropertyChanged));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM