2014-08-28 52 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 
{ 

} 
相关问题