Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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
C# 委派的命令不想工作_C#_Wpf_Xaml_Delegatecommand - Fatal编程技术网

C# 委派的命令不想工作

C# 委派的命令不想工作,c#,wpf,xaml,delegatecommand,C#,Wpf,Xaml,Delegatecommand,这是又一个漫长的编码日,我可能在寻找一些东西。 但是我不知道我错过了什么 ViewModel中的C#代码 public String MainWindowText { get; set; } public DelegateCommand CommandClose { get; set; } public TitlebarViewModel() { MainWindowText = "My Epic Window!"; CommandClose = new DelegateComm

这是又一个漫长的编码日,我可能在寻找一些东西。 但是我不知道我错过了什么

ViewModel中的C#代码

public String MainWindowText { get; set; }
public DelegateCommand CommandClose { get; set; }
public TitlebarViewModel()
{
    MainWindowText = "My Epic Window!";
    CommandClose = new DelegateCommand(CloseMain);
}
public void CloseMain(Object Sender)
{
    App.Current.MainWindow.Close();
}
public class DelegateCommand : ICommand
{
    private readonly Predicate<object> _canExecute;
    private readonly Action<object> _execute;
    public DelegateCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

    public virtual bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public virtual void Execute(object parameter)
    {
        _execute(parameter);
    }

    public void RaiseCanExecuteChanged()
    {
        if (CanExecuteChanged != null)
        {
            CanExecuteChanged(this, EventArgs.Empty);
        }
    }

    #pragma warning disable 67
    public event EventHandler CanExecuteChanged;
    #pragma warning restore 67
}
公共字符串MainWindowText{get;set;}
公共DelegateCommand命令close{get;set;}
公共标题栏视图模型()
{
MainWindowText=“我的史诗窗口!”;
CommandClose=新的DelegateCommand(CloseMain);
}
public void CloseMain(对象发送方)
{
App.Current.MainWindow.Close();
}
公共类DelegateCommand:ICommand
{
私有只读谓词_canExecute;
私有只读操作\u执行;
公共DelegateCommand(操作执行)
:此(执行,空)
{
}
公共DelegateCommand(操作执行,谓词canExecute)
{
_执行=执行;
_canExecute=canExecute;
}
公共虚拟布尔CanExecute(对象参数)
{
返回_canExecute==null?true:_canExecute(参数);
}
公共虚拟空执行(对象参数)
{
_执行(参数);
}
public void raisecancecutechanged()
{
如果(CanExecuteChanged!=null)
{
CanExecuteChanged(此为EventArgs.Empty);
}
}
#pragma警告禁用67
公共事件处理程序CanExecuteChanged;
#pragma警告恢复67
}
Xaml代码:

<Window.DataContext>
    <VM:TitlebarViewModel/>
</Window.DataContext>
<Grid>
    <DockPanel>
        <Image x:Name="Icon" Width="30" Height="30"/>
        <Button x:Name="Close" Content="X" Width="25" Height="25" DockPanel.Dock="Right" Margin="2" Command="{Binding CommandClose}"/>

        <TextBlock x:Name="TitleBarText" Text="{Binding MainWindowText}" TextAlignment="Center" Margin="7"/>
    </DockPanel>
</Grid>

因此,主窗口文本框显示的是我在c#构造函数中设置的内容,因此我知道datacontext工作正常。 我就是不明白为什么我不能在点击按钮时让授权命令触发

我知道这可能真的很愚蠢。这很简单,但我盯着这个屏幕看了50分钟,我真的在寻找错误。 特别是当我已经做了100次,并且它在我的解决方案中的其他控件中


谢谢各位。

按要求,好的。此应用程序使用activeX控件。这种控制存在空域问题。因此,我使用透明度将一个窗口与另一个窗口重叠

当你这样做的时候,你几乎需要重写整个移动/拖动的代码,或者任何一个窗口的代码。因此,我加入了一些代码,以允许“当我移动一个窗口时,移动它”

所以我用的是这个代码

    private void EventHandlers()
    {
        this.LocationChanged += Titlebar_LocationChanged;
        this.Loaded += Titlebar_Loaded;
        this.PreviewMouseLeftButtonDown += Titlebar_PreviewMouseLeftButtonDown;
        this.PreviewMouseLeftButtonUp += Titlebar_PreviewMouseLeftButtonUp;
        this.MouseDoubleClick += Titlebar_MouseDoubleClick;
    }
然而,我们必须得到警告。此事件将在触发窗体上的任何控件之前触发事件处理程序

我只是将事件处理程序改为

    private void EventHandlers()
    {
        this.LocationChanged += Titlebar_LocationChanged;
        this.Loaded += Titlebar_Loaded;
        this.MouseLeftButtonDown += Titlebar_PreviewMouseLeftButtonDown;
        this.PreviewMouseLeftButtonUp += Titlebar_PreviewMouseLeftButtonUp;
        this.MouseDoubleClick += Titlebar_MouseDoubleClick;
    }
按下控件中的任何按钮后,将触发此操作。 是的,对于有经验的程序员来说,这可能是愚蠢的。甚至我们有时也有内存块。
请不要评判我。

是否存在数据绑定错误?或者您可能在输出窗口中注意到的任何其他内容?不适用于此特定类。或者target Element好的,你能给我们提供一个可以复制相同的工作样品吗?我们可以看看这里吗。哦,等等,我知道了。实际上我是对的。然而,我有一个this.PreviewMouseLeftButtonDown事件处理程序,这样我就可以移动窗口,这就是将任何事件捕获到按钮。这就是导致它的原因。根据提供的代码,没有任何错误,我在一个示例应用程序中运行了它,它运行得很好