绑定后,从 UIElement 中查找 Bound 属性
案例:
public class customer
{
public string Adress { get; set; }
}
Xaml:
<堆栈面板>
。CS
公共 MainPage() { 初始化组件(); LayoutRoot.DataContext = new customer() { Adress = "Some Adr" }; }
所以问题是,在代码后面。如何获取绑定(地址)的属性(字符串)。我需要它来访问 customer.adress 作为属性,以分配另一个变量。 (在本例中,当事件发生时。例如,在 this.Loaded 发生之后。)
因此,我获得了 UIElement(发送者),并且可以从其 DataContext 中获取客户。
简而言之,如何获取绑定对象的属性名称。 (绑定对象很容易找到,我只是使用 DataContext
来获取客户,但是我在哪里可以获取来自发送者的 xaml 中的属性名称(例如名称))
(如果需要访问客户内部的“地址”,我计划使用反射),但如何获取文本框中文本所绑定的属性的“名称”。
Case:
public class customer
{
public string Adress { get; set; }
}
Xaml:
<Grid x:Name="LayoutRoot" Background="White" > <StackPanel> <TextBox Text="{Binding Adress}"/> </StackPanel> </Grid>
.cs
public MainPage() { InitializeComponent(); LayoutRoot.DataContext = new customer() { Adress = "Some Adr" }; }
So the Question is, in code behind. how do I get the property (string) of the bound (Adress). I need it to access the customer.adress as a property, to assign another variable. (in this case when a event occours. e.g. after the this.Loaded occurs.)
So I got the UIElement
(sender) and I can get the customer form its DataContext
.
In short, how do I get the property name of the binding object. (the binding object is easy to find I just use DataContext
to get the customer, but where can I get the name of the property? that are in the xaml (eg. name) from the sender)
(I plan to use reflection if it is nessesery to access the "adress" inside customer) but how to get the "name" of the property that text in the textBox is bound to.
尝试以下操作:
为您的
TextBox
指定一个名称,以便您可以从代码中访问它:在代码隐藏中:
现在可以从以下位置将绑定属性的名称作为字符串提取:
Try the following:
Give your
TextBox
a name, so you can access it from code:In code behind:
The name of the bound property can now be extracted as a string from: