C# 为什么我的WPF绑定实现不能双向工作?

C# 为什么我的WPF绑定实现不能双向工作?,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我正在尝试为WPF绑定创建一个模板,以便将来需要使用绑定时可以从中提取代码。当前绑定无论哪种方式都不起作用 我希望这段代码在UI的文本框中显示“MyString”和“MyInt”,并在用户更改值时正确更改逻辑(因此是复选按钮) 但是,MyString和MyInt不会显示在文本框中,更改它们也不会实际更改变量的值 MainWindow.xaml.cs: using System.Windows; namespace WpfBindingTemplate { public partial c

我正在尝试为WPF绑定创建一个模板,以便将来需要使用绑定时可以从中提取代码。当前绑定无论哪种方式都不起作用

我希望这段代码在UI的文本框中显示“MyString”和“MyInt”,并在用户更改值时正确更改逻辑(因此是复选按钮)

但是,MyString和MyInt不会显示在文本框中,更改它们也不会实际更改变量的值

MainWindow.xaml.cs:

using System.Windows;
namespace WpfBindingTemplate
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            myController = new Controller();
        }

        Controller myController;

        private void check_string(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("MyString: " + myController.MyString);
        }

        private void check_int(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("MyInt: " + myController.MyInt);
        }
    }
}
MainWindow.xaml:

<Window x:Class="WpfBindingTemplate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfBindingTemplate"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Name="myTextBox" Text="{Binding Path=myController.MyString}" HorizontalAlignment="Left" Height="23" Margin="202,136,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Label x:Name="label" Content="String binding:" HorizontalAlignment="Left" Margin="77,133,0,0" VerticalAlignment="Top" Width="104"/>
        <Button x:Name="button" Content="string check" Click="check_string" HorizontalAlignment="Left" Margin="370,136,0,0" VerticalAlignment="Top" Width="75"/>
        <Label x:Name="label1" Content="int binding:" HorizontalAlignment="Left" Margin="77,188,0,0" VerticalAlignment="Top" Width="94"/>
        <TextBox x:Name="textBox" Text="{Binding Path=myController.MyInt}" HorizontalAlignment="Left" Height="23" Margin="202,190,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Button x:Name="button1" Click="check_int" Content="int check" HorizontalAlignment="Left" Margin="370,188,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</Window>

控制器类:

using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace WpfBindingTemplate
{
    public class Controller : INotifyPropertyChanged
    {
        public Controller()
        {
            MyString = "test string";
            MyInt = 1;
        }

        public int MyInt { get; set; }

        private string myString;
        public string MyString { get { return myString; } set { SetProperty(ref myString, value); } }


        public event PropertyChangedEventHandler PropertyChanged;
        private void SetProperty<T>(ref T field, T value, [CallerMemberName] string name = "")
        {
            if (!EqualityComparer<T>.Default.Equals(field, value))
            {
                field = value;
                var handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(name));
                }
            }
        }
    }
}
使用System.Collections.Generic;
使用系统组件模型;
使用System.Runtime.CompilerServices;
命名空间WpfBindingTemplate
{
公共类控制器:INotifyPropertyChanged
{
公共控制员()
{
MyString=“测试字符串”;
MyInt=1;
}
公共int MyInt{get;set;}
私有字符串myString;
公共字符串MyString{get{return MyString;}set{SetProperty(ref MyString,value);}
公共事件属性更改事件处理程序属性更改;
私有void SetProperty(ref T字段,T值,[CallerMemberName]字符串名称=”)
{
如果(!EqualityComparer.Default.Equals(字段,值))
{
字段=值;
var handler=PropertyChanged;
if(处理程序!=null)
{
处理程序(此,新PropertyChangedEventArgs(名称));
}
}
}
}
}

对于初学者来说,绑定对字段不起作用,您需要在“Controller myController”上设置get/set;它还需要是公共的(在某些情况下,它也可以与内部绑定一起使用)