Я сейчас изучаю привязку в 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.cs]
namespace WpfApp2
{
public class Class1
{
public bool Property { get; set; }
}
}
Что не так?Я не могу догадаться ... Могу ли я сделать это только в коде?Пожалуйста, помогите мне!