Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF-用户控件之间的命令_Wpf - Fatal编程技术网

WPF-用户控件之间的命令

WPF-用户控件之间的命令,wpf,Wpf,我想将命令绑定到我的用户控件: <MyUserControl MyCommand="{Binding TestCommand}"/> MyUserControl.xaml <Button Content="CLICK" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=MyCommand

我想将命令绑定到我的用户控件:

<MyUserControl MyCommand="{Binding TestCommand}"/>
MyUserControl.xaml

<Button Content="CLICK" 
                Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=MyCommand}"/>
父用户控件:


当我点击按钮时,命令不执行。感谢您的建议

我试图重现这个问题,但我无法重现,我的发现可能会让您错过数据上下文

主窗口


从上下文中删除的两行xaml标记不是一个简单的标记。许多wpf问题都是由于视图和viewmodel的接线不正确造成的。因此,请使您的示例完整且可复制DependencyProperty的第三个参数。Register必须是注册属性的类型,即typeofMyUserControl。什么是ToggleButtons?定义命令属性的类型的实际名称是什么?如何定义要执行的实际命令?
<Button Content="CLICK" 
                Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=MyCommand}"/>
 <view:MyUserControl Grid.Row="0" Grid.Column="1" MyCommand="{Binding TestCommand}"/>
 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            testCommand = new RelayCommand(ChangeCanExecute);
            this.DataContext = this;
        }

        private ICommand testCommand { get; set; }
        private bool canExecute = true;
        public bool CanExecute
        {
            get
            {
                return this.canExecute;
            }

            set
            {
                if (this.canExecute == value)
                {
                    return;
                }

                this.canExecute = value;
            }
        }

        public ICommand TestCommand
        {
            get
            {
                return testCommand;
            }
            set
            {
                testCommand = value;
            }
        }
        public void ChangeCanExecute(object obj)
        {
            canExecute = !canExecute;
        }
    }