2017-08-10 80 views
-4

如何查找TextBox MVVM属性名称在运行系统上与TextBox绑定的属性名称。查找基于TextBox名称的运行时的TextBox的MVVM属性名称Wpf

<TextBox Name="txtCityName" Grid.Row="3" Grid.Column="1" Text="{Binding CityName, UpdateSourceTrigger=PropertyChanged}" Height="40" Width="200"/> 

<TextBox Name="CountryName" Text="None" Height="40" Width="200" Margin="159,184,158,95"/> 

上面是我的两个文本框。与物业绑定。 在代码后面当我运行我的Wpf应用程序。

我把所有的TextBox名称放到一个文件中。一旦文件加载,并给我 文本框的名字,我想获得MVVM属性名称上运行时

+0

这个问题并不清楚。 –

回答

0

你可以检查TextBox是数据绑定使用GetBindingExpression方法:

var be = txtCityName.GetBindingExpression(TextBox.TextProperty); 
if(be != null && be.ParentBinding != null) 
{ 
    string sourceProperty = be.ParentBinding.Path.Path; 
} 
+0

谢谢。做得好。你节省了我的时间 –