C# 未能在WPF中绑定

C# 未能在WPF中绑定,c#,wpf,C#,Wpf,我现在正在学习WPF中的绑定。 我想将Button.IsEnabled属性绑定到Class1的属性;Button.IsEnabled=c1.Property。事实上,Textblock.Text按我的预期更改,但Button.IsEnabled没有更改。 这是我的密码: [MainWindow.xaml.cs] using System.Windows; namespace WpfApp2 { public partial class MainWindow : Window {

我现在正在学习WPF中的绑定。
我想将Button.IsEnabled属性绑定到Class1的属性;Button.IsEnabled=c1.Property。事实上,Textblock.Text按我的预期更改,但Button.IsEnabled没有更改。
这是我的密码:

[MainWindow.xaml.cs]

using System.Windows;

namespace WpfApp2
{
    public partial class MainWindow : Window
    {
        public static Class1 c1 { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            c1 = new Class1();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            c1.Property = !c1.Property;
            textblock.Text = c1.Property.ToString();
        }
    }
}
[MainWindow.xaml]

<Window x:Class="WpfApp2.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:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="150,137,0,0" VerticalAlignment="Top" Width="75" IsEnabled="{Binding Property, Source={x:Static local:MainWindow.c1}}"/>
        <Button Content="Button2" HorizontalAlignment="Left" Margin="271,137,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
        <TextBlock x:Name="textblock" HorizontalAlignment="Left" Margin="150,177,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>
    </Grid>
</Window>

怎么了?我猜不出。。。我只能用代码来做吗?请帮帮我

您需要在
Class1
中实现
INotifyPropertyChanged
接口,并在
属性值更改时引发其
PropertyChanged
事件:

namespace WpfApp2
{
    public class Class1 : INotifyPropertyChanged
    {
        private bool property = false;

        public bool Property
        {
            get { return property; }
            set { if (value != property) { property = value; RaisePropertyChanged(); } }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged([CallerMemberName] string propertyName = "")
            => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName);
    }
}

除了答案中解释的内容外,不要在主窗口中使用静态c1成员作为绑定源。相反,创建一个非静态成员,并设置
DataContext=c1在主窗口构造函数中。然后从绑定中删除源代码,只需写入
IsEnabled=“{Binding Property}”
。花点时间阅读以下内容:。感谢您的详细回答!
namespace WpfApp2
{
    public class Class1 : INotifyPropertyChanged
    {
        private bool property = false;

        public bool Property
        {
            get { return property; }
            set { if (value != property) { property = value; RaisePropertyChanged(); } }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged([CallerMemberName] string propertyName = "")
            => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName);
    }
}