根据 WPF 中的文本框名称在运行时查找文本框的 MVVM 属性名称



如何查找文本框 MVVM 属性名称,文本框在运行时绑定了谁。

<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 应用程序时的代码隐藏中。

我将所有文本框名称放入一个文件中。加载文件后给我 文本框名称我想在运行时获取 MVVM 属性名称

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

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

最新更新