你好,我有关于wpf/xaml文本框c#实现的问题。
我试图在我的c#代码中了解如何使用UpdateSourceTrigger。我是个新手,所以如果人们对我有耐心和帮助,我会非常感激。
在我的C#中,我需要知道文本框中的数据是如何使用UpdateSourceTrigger访问的。我知道在调用OnPropertyChanged()时属性发生了变化。但是,如果用户试图在LostFocus或PropertyChanged代码中使用C#,我还需要知道如何使用。这样我就可以对这两种情况做一些特殊的处理。
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时,还有其他方法被调用吗?
谢谢你
发布于 2014-08-28 02:00:11
您必须获得对TextBlock的引用并获得绑定表达式,然后才能访问Binding信息。
示例:(没有错误/空检查)
<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"发布于 2014-08-28 02:13:16
获取绑定对象的另一种方法是:
Binding binding = BindingOperations.GetBinding(myTextBox, TextBox.TextProperty);
if (binding.UpdateSourceTrigger.ToString().Equals("LostFocus"))
{
}
else
{
}https://stackoverflow.com/questions/25539506
复制相似问题