2014-08-28 51 views
0

你好,我有一個wpf/xaml文本框c#實現的問題。TextBox UpdateSourceTrigger PropertyChanged和LostFocus

我想弄清楚如何在我的C#代碼中知道UpdateSourceTrigger正在使用什麼。 我是一個新手,所以我非常感謝,如果人們對我有耐心和樂於助人。

在我的C#中,我需要知道文本框中的數據如何使用UpdateSourceTrigger訪問。當我調用OnPropertyChanged()時,我知道屬性發生了改變。但是我也需要知道如果用戶試圖在C#代碼中使用LostFocus或PropertyChanged。這樣我就可以爲這兩種情況做一些特殊的處理。

XAML

<TextBox> 
    <TextBox.Text> 
     <Binding Source="{StaticResource myDataSource}" Path="Name" 
     UpdateSourceTrigger="PropertyChanged"/> 
    </TextBox.Text> 
</TextBox>  

C#

protected void OnPropertyChanged(string name) 
{ 
    // If UpdateSourceTrigger= PropetyChanged then process one way 
    // If UpdateSourceTrigger= LostFocus then process one way 
} 

是否有任何其他的是開始使用LostFocus在調用的方法?

感謝您

回答

2

您將獲得一個關於你TextBlock並獲得綁定表達式,那麼你將有機會獲得Binding信息

例子:(無錯誤/ null的檢查)

<TextBox x:Name="myTextblock"> 
    <TextBox.Text> 
     <Binding Source="{StaticResource myDataSource}" Path="Name" 
     UpdateSourceTrigger="PropertyChanged"/> 
    </TextBox.Text> 
</TextBox> 


var textblock = this.FindName("myTextBlock") as TextBlock; 
var trigger = textblock.GetBindingExpression(TextBlock.TextProperty).ParentBinding.UpdateSourceTrigger; 
// returns "PropertyChanged" 
1

獲取綁定對象的另一種方式是:

Binding binding = BindingOperations.GetBinding(myTextBox, TextBox.TextProperty); 

if (binding.UpdateSourceTrigger.ToString().Equals("LostFocus")) 
{ 

} 
else 
{ 

} 
相關問題