添加命令后禁用按钮

时间:2019-06-19 10:56:15

标签: wpf wpf-controls custom-controls

可能看起来很傻。当命令绑定时,无法确定此处的按钮示例代码,

我有自定义控件,

public class Stepper: ListBox
{
        /// <summary>
        /// Internal command used by the XAML template (public to be available in the XAML template). Not intended for external usage.
        /// </summary>
        public static readonly RoutedCommand BackCommand = new RoutedCommand();

        static Stepper()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(UXStepper), new FrameworkPropertyMetadata(typeof(Stepper)));
        }

        public UXStepper()
        {
            CommandBindings.Add(new CommandBinding(BackCommand, (o, e) => ZoomFit(), (o, e) => e.CanExecute = true));
        }

        private void ZoomFit()
        {
            //some implementation
        }
}

XAML代码

 <Button HorizontalAlignment="Center" 
         VerticalAlignment="Center"
         Content="Back"
         Command="{x:Static shui:Stepper.BackCommand}"/>

1 个答案:

答案 0 :(得分:0)

如果我理解正确:您的问题是绑定无效

  • 如果您要绑定某些具有静态属性的东西,而不必定义源
<Window.Resources>
    <shui:Stepper x:Key="stepper"/>
</Window.Resources>

<Button HorizontalAlignment="Center" 
        VerticalAlignment="Center"
        Content="Back"
        Command="{BackCommand, Source={StaticResource stepper}}"/>

  • 或者您可以将其绑定到您的Singleton(如果有的话)

<Button HorizontalAlignment="Center" 
        VerticalAlignment="Center"
        Content="Back"
        Command="{BackCommand, Source={x:Static shui:Stepper.Instance}}"/>