【发布时间】:2018-01-03 02:29:22
【问题描述】:
有没有办法使用绑定到它们的属性来获取 XAML 中的控件,
类似这样的:
Account acc = GetFromDB(id);
foreach (var prop in acc.GetType().GetProperties())
{
Control control = GetControl(prop.Name);
//Then I want to set properties like Foreground for the control
}
private Control GetControl(string propertyName)
{
//getting the control bound to the propertyName
}
并说这是 XAML 代码:
<Label>Name</Label>
<TextBox Name="txtName"
Grid.Column="1"
Text="{Binding Name}"/>
<Label Grid.Row="1">Debt</Label>
<TextBox Name="txtDebt"
Grid.Column="1"
Grid.Row="1"
Text="{Binding Debt}"/>
<Label Grid.Row="2">Join Date</Label>
<DatePicker Name="dpJoin"
Grid.Column="1"
Grid.Row="2"
Text="{Binding JDate}"/>
【问题讨论】:
-
在控件上设置名称属性,如
<TextBox x:Name="myTextBox" />,现在您可以在代码中使用此名称访问此控件,如myTextBox.Width = 300; -
我在上面的代码中设置了,现在跳过那个解决方案!