[英]TextBox UpdateSourceTrigger for PropertyChanged and LostFocus
Hello I have question about a wpf/xaml text box c# implementation. 您好,我对WPF / XAML文本框C#实现有疑问。
I am trying to figure out how in my c# code to know what the UpdateSourceTrigger is being used. 我试图弄清楚如何在我的C#代码中知道正在使用什么UpdateSourceTrigger。 I am a newbie, so I would very much appreciate if people are patient with me and helpful. 我是新手,所以如果人们对我耐心并且乐于助人,我将不胜感激。
In my C# I need to know how the data in the Text box is trying to be accessed using UpdateSourceTrigger. 在我的C#中,我需要知道如何尝试使用UpdateSourceTrigger访问“文本”框中的数据。 I know the property changed when my OnPropertyChanged() is called. 我知道调用OnPropertyChanged()时属性已更改。 But I also need to know how if the user is trying to use LostFocus or PropertyChanged in the C# code. 但是我还需要知道用户是如何尝试在C#代码中使用LostFocus或PropertyChanged。 This is so I can do some special processing for either case. 这样,无论哪种情况我都可以做一些特殊处理。
xaml a
<TextBox>
<TextBox.Text>
<Binding Source="{StaticResource myDataSource}" Path="Name"
UpdateSourceTrigger="PropertyChanged"/>
</TextBox.Text>
</TextBox>
c# C#
protected void OnPropertyChanged(string name)
{
// If UpdateSourceTrigger= PropetyChanged then process one way
// If UpdateSourceTrigger= LostFocus then process one way
}
Is there any other methods that get called when using LostFocus? 使用LostFocus时还有其他方法可以调用吗?
Thanks you 谢谢
You will have to get a reference to your TextBlock
and get the binding expression then you will have access to the Binding
information 您将必须获取对TextBlock
的引用并获取绑定表达式,然后才能访问Binding
信息。
Example:(no error/null checking) 示例:(无错误/空检查)
<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"
another way of getting the binding object is: 获取绑定对象的另一种方法是:
Binding binding = BindingOperations.GetBinding(myTextBox, TextBox.TextProperty);
if (binding.UpdateSourceTrigger.ToString().Equals("LostFocus"))
{
}
else
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.