【问题标题】:Very Simple "Dynamic" Control Creation and Binding WPF C#非常简单的“动态”控件创建和绑定 WPF C#
【发布时间】: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 {  }
            //}
        }

【问题讨论】:

标签: c# wpf binding textbox


【解决方案1】:

这是一个示例,您将如何使用 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#-代码:

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

    }
}

【讨论】:

    【解决方案2】:

    你的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));
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-15
        • 1970-01-01
        • 2011-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-10
        • 2011-02-13
        相关资源
        最近更新 更多