Datepicker 在绑定中禁用或启用复选框

Datepicker disable or enable with checkbox in binding

我想通过复选框启用或禁用日期选择器,但我的代码不起作用。

这是xaml

<CheckBox HorizontalAlignment="Left" Height="32" 
    IsChecked="{Binding ABC}"
    Margin="108,91,0,0" VerticalAlignment="Top" Width="191"/>


<DatePicker HorizontalAlignment="Left" Height="32" 
    IsEnabled="{Binding ABC}"
    Margin="142,91,0,0" VerticalAlignment="Top" Width="217"/>

这是 属性。

private bool _ABC {
    get;
    set;
}

public bool ABC {
    get {
        return _ABC;
    }
    set {
        _ABC = value;
    }
}

private void Window_Loaded(object sender, RoutedEventArgs e) {
    MainWindow Vm = new MainWindow();
    this.DataContext = Vm;

    ABC = false;
}

谢谢

您需要告诉它 属性 它在寻找什么,以及从谁那里得到它,例如;

<CheckBox x:Name="TheCheckBox"
          HorizontalAlignment="Left" Height="32" 
          IsChecked="{Binding ABC}"
          Margin="108,91,0,0" VerticalAlignment="Top" Width="191"/>


<DatePicker HorizontalAlignment="Left" Height="32" 
            IsEnabled="{Binding IsChecked, ElementName=TheCheckBox}"
            Margin="142,91,0,0" VerticalAlignment="Top" Width="217"/>

希望这对你有帮助,干杯。