【问题标题】:Find MVVM Property Name of TextBox On Runtime based On TextBox Name in Wpf根据 Wpf 中的 TextBox 名称在运行时查找 TextBox 的 MVVM 属性名称
【发布时间】:2017-08-10 11:58:55
【问题描述】:

如何在运行时查找 TextBox 绑定的 TextBox 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 应用程序时的代码中。

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

【问题讨论】:

  • 问题不清楚。

标签: wpf xaml


【解决方案1】:

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

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

【讨论】:

  • 谢谢。做得好。你节省了我的时间
猜你喜欢
  • 2011-09-01
  • 2020-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-15
  • 1970-01-01
  • 2018-11-05
  • 2017-10-23
相关资源
最近更新 更多