C# 通过绑定属性获取XAML控件?
是否有一种方法可以使用绑定到XAML中的属性获取控件 大概是这样的:C# 通过绑定属性获取XAML控件?,c#,wpf,xaml,C#,Wpf,Xaml,是否有一种方法可以使用绑定到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
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
}
var frame = Window.Current.Content as Frame;
var textBox = VisualHelper.GetChildOfType<TextBox>(frame);
var anotherTextBox = VisualHelper.FindVisualChildByName<TextBox>(frame, "MyTextBox");
var obj = VisualHelper.FindVisualChildByName<object>(frame, "MyTextBox");
var type = obj.GetType();
var textBox = System.Convert.ChangeType(obj, type);
假设这是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}"/>
名称
债务
加入日期
只需设置x:Name
并通过找到所需的控件即可。
如果需要特定的控件名,可以像x:name=“{Binding name}”
或者像这样铸造:
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
}
var frame = Window.Current.Content as Frame;
var textBox = VisualHelper.GetChildOfType<TextBox>(frame);
var anotherTextBox = VisualHelper.FindVisualChildByName<TextBox>(frame, "MyTextBox");
var obj = VisualHelper.FindVisualChildByName<object>(frame, "MyTextBox");
var type = obj.GetType();
var textBox = System.Convert.ChangeType(obj, type);
var obj=VisualHelper.FindVisualChildByName(框架,“MyTextBox”);
var type=obj.GetType();
var textBox=System.Convert.ChangeType(对象,类型);
只需设置x:Name
并通过找到所需的控件即可。
如果需要特定的控件名,可以像x:name=“{Binding name}”
或者像这样铸造:
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
}
var frame = Window.Current.Content as Frame;
var textBox = VisualHelper.GetChildOfType<TextBox>(frame);
var anotherTextBox = VisualHelper.FindVisualChildByName<TextBox>(frame, "MyTextBox");
var obj = VisualHelper.FindVisualChildByName<object>(frame, "MyTextBox");
var type = obj.GetType();
var textBox = System.Convert.ChangeType(obj, type);
var obj=VisualHelper.FindVisualChildByName(框架,“MyTextBox”);
var type=obj.GetType();
var textBox=System.Convert.ChangeType(对象,类型);
设置控件的Name属性,如
,现在您可以使用此名称在代码隐藏中访问此控件,如myTextBox.Width=300代码>我在上面的代码中设置了它,现在跳过该解决方案!这正是我想要的:在控件上设置Name属性,如
,现在您可以使用此名称在代码隐藏中访问此控件,如myTextBox.Width=300代码>我在上面的代码中设置了它,现在跳过该解决方案!这正是我想要的:你能解释一下你的代码吗?我想要的是一个只接受一个字符串值(即属性名称)并返回绑定到该属性的控件的方法请参见问题的编辑器,但控件可能是文本框
,复选框
,日期选择器
。。等等。我只是不知道属性和它的绑定控制!下面是一个完整的示例,您可以尝试通过System.Convert.ChangeType(obj,type)强制转换控件代码>再次查看编辑请解释您的代码,我所期望的是一个只接受字符串值(即属性名称)并返回绑定到该属性的控件的方法参见问题的编辑,但该控件可能是文本框
,复选框
,日期选择器
。。等等。我只是不知道属性和它的绑定控制!下面是一个完整的示例,您可以尝试通过System.Convert.ChangeType(obj,type)强制转换控件代码>请参见再次编辑