我想做的是以下
像这样创建一个UserControle:
public partial class MyUserControle : UserControl
内置Wrappanel,将控件和内容添加到
<WrapPanel Margin="0,0,0,41" Name="wrapPanel1" />
此类有2个按钮可以取消或在窗口中应用设置。 窗口稍后会像这样打开
Window w = new Window();
w.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
w.Content = myClass.getmyUserControle();
bool? t = w.ShowDialog();
其中myClass是一个特殊的类,它创建UserControle并添加一些想要在WrapPanel中更改的东西
现在在myClass中说我有一个简单的Point
System.Drawing.Point myPoint = new Point(10,15);
和getmyUserControle()看起来像
public MyUserControle getmyUserControle(){
MyUserControle UC = new MyUserControle();
WPF.TextBox X1 = new WPF.TextBox();
System.Windows.Data.Binding BindingX = new System.Windows.Data.Binding("X");
BindingX.Source = myPoint;
BindingX.Mode = System.Windows.Data.BindingMode.TwoWay;
X1.SetBinding(WPF.TextBox.TextProperty, BindingX);
UC.addhere.Add(X1);
return UC;
}
文本框已正确填充,但更改不会更改源。我该如何解决这个问题?
编辑: 我可以添加一个
private MyUserControle myUCsave = null;
到班级并设置
myUCsave = UC;
在getmyUserControle()的末尾,并在开始时检查
myUCsave != null return myUCsave;
修复了文本框的“保存”,但底层的myPoint没有改变。
编辑:也许有一个更简单的案例:我创建一个简单的表单并通过构造函数将其添加到MainWindow
public partial class MainWindow : Window
public MainWindow()
{
InitializeComponent();
TextBox X1 = new TextBox();
TextBox Y1 = new TextBox();
X1.Margin = new Thickness(0, 0, 20, 20);
Y1.Margin = new Thickness(0, 0, 100, 100);
X1.Width = 100;
Y1.Width = 100;
X1.Height = 200;
Y1.Height = 200;
System.Windows.Data.Binding BindingX = new System.Windows.Data.Binding("X");
System.Windows.Data.Binding BindingY = new System.Windows.Data.Binding("X");
BindingX.Mode = System.Windows.Data.BindingMode.TwoWay;
BindingY.Mode = System.Windows.Data.BindingMode.TwoWay;
BindingX.Source = myPoint;
BindingY.Source = myPoint;
X1.SetBinding(TextBox.TextProperty, BindingX);
Y1.SetBinding(TextBox.TextProperty, BindingY);
this.MainGrid.Children.Add(Y1);
this.MainGrid.Children.Add(X1);
}
Point myPoint = new Point(100, 200);
这应该创建两个链接到同一源(myPoint.X)的文本框X1,Y1吗?但是,当我改变一件事时,其他文本框也不会改变源。