如何在代码中将 TextBoxes 绑定到 Properties.Settings.Default 中的每个属性? [英] How can I bind TextBoxes to each property in Properties.Settings.Default in code?
本文介绍了如何在代码中将 TextBoxes 绑定到 Properties.Settings.Default 中的每个属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想动态生成一个文本框视图,显示Properties.Settings.Default
中每个属性的名称和值.
I would like to dynamically generate a view of text boxes that shows the names and values of each property in Properties.Settings.Default
.
我尝试了以下代码,但无法正确分配路径.
I have tried the following code, but I'm unable to get the path to assign correctly.
foreach (SettingsProperty currentProperty in Properties.Settings.Default.Properties)
{
TextBox X = new TextBox();
TextBox Y = new TextBox();
Grid G = new Grid();
var x = Properties.Settings.Default[currentProperty.Name];
X.SetBinding(ItemsControl.ItemsSourceProperty,
new Binding
{
Source = "{x:Static properties:Settings.Default}" + ", Path = " + currentProperty.Name + "}",
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
//{Binding Source={x:Static properties:Settings.Default}, Path = MYSETTING1}
G.Children.Add(X);
MyStackPanel.Children.Add(G);
}
我还尝试了以下方法:
X.SetBinding(ItemsControl.ItemsSourceProperty,
new Binding
{
Source = "{x:Static properties:Settings.Default}",
Path = currentProperty.Name,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
推荐答案
离开你最初的句子,你可以做这样的事情:
Just going off your initial sentence, you could do something like this:
foreach (SettingsProperty currentProperty in Properties.Settings.Default.Properties)
{
TextBox X = new TextBox();
TextBox Y = new TextBox();
Grid G = new Grid();
ColumnDefinition one = new ColumnDefinition();
ColumnDefinition two = new ColumnDefinition();
one.Width = new GridLength(50, GridUnitType.Star);
two.Width = new GridLength(50, GridUnitType.Star);
G.ColumnDefinitions.Add(one);
G.ColumnDefinitions.Add(two);
Grid.SetColumn(X, 0);
Grid.SetColumn(Y, 1);
X.SetBinding(TextBox.TextProperty,
new Binding
{
Source = currentProperty.Name,
Path = new PropertyPath("."),
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
Y.SetBinding(TextBox.TextProperty,
new Binding
{
Source = currentProperty.DefaultValue,
Path = new PropertyPath("."),
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
G.Children.Add(X);
G.Children.Add(Y);
MyStackPanel.Children.Add(G);
}
这篇关于如何在代码中将 TextBoxes 绑定到 Properties.Settings.Default 中的每个属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文