a.xaml
<Grid x:Name="root"> <TextBox Text="{Binding str1}" Width="200" Height="25" Margin="92,23.5,92,0" VerticalAlignment="Top" d:LayoutOverrides="Height"/> <Button IsEnabled="{Binding YN}" Margin="92,106,92,116" Content="按钮"/> <TextBox Text="{Binding str2}" Width="200" Height="25" Margin="92,0,92,28.5" VerticalAlignment="Bottom" d:LayoutOverrides="Height"/> </Grid>
a.xaml.cs
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e) { //apps.Resize(root.ActualWidth, root.ActualHeight); } private void Window_Loaded(object sender, RoutedEventArgs e) { //apps.Resize(root.ActualWidth, root.ActualWidth); root.DataContext = new BingO() { str1 = "qq", str2 = "ww2" }; } } public class BingO { public string str1 { get; set; } public string str2 { get; set; } public bool YN { get { return str1.Length > str2.Length; } } }
如何定义 BingO 的属性 才能让正常 工作? 根据str1, str2 的实时输入而改变 按钮是否可用?
实现INotifyPropertyChenged接口
BingO的属性申明没有属性值的改变通知事件
你的 BingO类 实现INotifyPropertyChenged接口。当str1和str2值变化的时候 通知YN
只接在str1和str2两个TextBox 的PropertyChenged事件里设置按钮是否可用。比 BingO类继承INotifyPropertyChenged接口容易多啦!