Very simple & ldquo; Dynamic & rdquo; Creation and Link Control WPF C #

advertisements

I have a VERY simple Window. And a Very simple Point. And a very Simple TextBox. But i cant bind it. The Text from the Point is created in the Textbox. So one Way Works. But the way back to Source doesnt. The Button should show at least the update Point. Please Help

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>

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

    }
}

Why Does the Textbox not Update the source? Ok.. so struct doesnt work. Now the binding doesnt work at all ... I created a simple Pointclass and now the point doesnt get shown...

Final Update:

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


Binding just work with Public Properties. so pls change your fields into properties and try to define bindings in xaml. it is much more easier to read :)