【发布时间】:2015-01-07 20:47:58
【问题描述】:
我有一个非常简单的窗口。还有一个非常简单的观点。和一个非常简单的文本框。但我不能绑定它。来自点的文本是在文本框中创建的。所以一种方法有效。但是回到源头的方式没有。按钮应至少显示更新点。请帮忙
我的 xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Name="MainGrid">
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="416,27,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>
我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
myPoint = new myPointClass(100, 200);
this.DataContext = this;
TextBox X1 = new TextBox();
TextBox Y1 = new TextBox();
X1.Margin = new Thickness(0, 0, 20, 20);
Y1.Margin = new Thickness(0, 0, 200, 200);
X1.Width = 100;
Y1.Width = 100;
X1.Height = 50;
Y1.Height = 50;
System.Windows.Data.Binding BindingX = new System.Windows.Data.Binding("X");
System.Windows.Data.Binding BindingY = new System.Windows.Data.Binding("Y");
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(X1);
this.MainGrid.Children.Add(Y1);
}
public myPointClass myPoint;
private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(myPoint.X + "=X | Y=" + myPoint.Y);
}
public class myPointClass
{
public int X;
public int Y;
public myPointClass(int X, int Y)
{
this.X = X; this.Y = Y;
}
}
}
}
为什么文本框不更新源? 好的..所以结构不起作用。 现在绑定根本不起作用...我创建了一个简单的 Pointclass,现在该点没有显示...
最终更新:
public class myPointClass// : System.ComponentModel.INotifyPropertyChanged
{
public int X { get; set; }
public int Y { get; set; }
public myPointClass(int X, int Y)
{
this.X = X; this.Y = Y;
}
//event System.ComponentModel.PropertyChangedEventHandler System.ComponentModel.INotifyPropertyChanged.PropertyChanged
//{
// add { }
// remove { }
//}
}
【问题讨论】:
-
谢谢。我用点类更新了代码。但是现在绑定根本不起作用。