非常简单”;“动态”;控件创建和绑定WPF C#

本文关键字:绑定 WPF 创建 动态 简单 非常 控件 | 更新日期: 2023-09-27 18:30:02

我有一个非常简单的窗口。还有一个非常简单的观点。还有一个非常简单的TextBox。但我无法绑定它。来自点的文本是在文本框中创建的。所以一种方法是有效的。但回到源头的路并没有。按钮应至少显示更新点。请帮助

my-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 {  }
            //}
        }

非常简单”;“动态”;控件创建和绑定WPF C#

绑定只适用于公共属性。因此,请将您的字段更改为属性,并尝试在xaml中定义绑定。它更容易阅读:)

myPointClass需要继承INotifyPropertyChanged并实现NotifyPropertyChanged,您必须指定由哪个方法触发的源。

BindingX.UpdateSourceTrigger=UpdateSourceTriger.PropertyChanged;BindingY.UpdateSourceTrigger=UpdateSourceTriger.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));
        }
    }
}

以下是如何使用XAML绑定实现这一点的示例:

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#-代码:

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;
            }
        }
    }
}