我有一个非常简单的窗口。还有一个非常简单的要点。还有一个非常简单的TextBox。但我无法绑定它。点中的文本在文本框中创建。所以单向工作。但回到Source的方式并不重要。 Button应该至少显示更新点。请帮忙
我的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 { }
//}
}
答案 0 :(得分:1)
绑定只需使用公共属性。所以请将您的字段更改为属性并尝试在xaml中定义绑定。它更容易阅读:)
答案 1 :(得分:0)
你的myPointClass需要继承INotifyPropertyChanged并实现NotifyPropertyChanged,你必须指定你的源由哪种方法触发。
BindingX.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; BindingY.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
public partial class MainWindow : Window
{
public MyPoint myPoint = new MyPoint(100, 200);
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;
X1.DataContext = myPoint;
Y1.DataContext = myPoint;
System.Windows.Data.Binding BindingX = new System.Windows.Data.Binding("X");
System.Windows.Data.Binding BindingY = new System.Windows.Data.Binding("Y");
BindingX.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingY.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
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);
X1.Text = "1";
}
private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(myPoint.X + "=X | Y=" + myPoint.Y);
//myPoint.Y = 1123;
//myPoint.X = 3123;
}
}
public class MyPoint : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private double _x;
private double _y;
public double X { get { return _x; } set { _x = value; NotifyPropertyChanged("X"); } }
public double Y { get { return _y; } set { _y = value; NotifyPropertyChanged("Y"); } }
public MyPoint(double x, double y)
{
X = x;
Y = y;
}
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
答案 2 :(得分:0)
以下是如何使用XAML-Bindings实现此目的的示例:
XAML的代码:
<Window x:Class="WpfApplication3.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">
<TextBox Name="X1" Height="32" VerticalAlignment="Bottom" Margin="38,0,324,191" Text="{Binding X, Mode=TwoWay}"/>
<TextBox Name="Y1" Margin="38,64,324,228" Text="{Binding Y, Mode=TwoWay}"/>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="416,27,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>
C#-Code:
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 WpfApplication3
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
DataContext = new myPointClass(100, 200);
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(((myPointClass)DataContext).X + "=X | Y=" + ((myPointClass)DataContext).Y);
}
public class myPointClass
{
public int X { get; set; }
public int Y { get; set; }
public myPointClass(int x, int y)
{
this.X = x; this.Y = y;
}
}
}
}