1

それはまた長いコーディングの日であり、私はおそらく何かを見過ごしています。しかし、私は逃したものを理解することはできません

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
}

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>

そのため、MainWindow テキスト ボックスは、C# コンストラクターで設定したものから表示されているので、データ コンテキストが正常に機能していることがわかります。ボタンをクリックしたときに委任されたコマンドを起動できない理由がわかりません。

私はこれがおそらく本当にばかげていることを知っています。それは単純なことですが、この画面を50分間見つめていて、エラーを探しすぎています。特に私がこれを100回行ったとき、それは私のソリューション全体の他のコントロールにあります

みんなありがとう。

4

1 に答える 1

0

リクエスト通りでOK。このアプリケーションは、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;
    }

これは、コントロール内の任意のボタンを押すとトリガーが発生します。はい、これは経験豊富なプログラマーに見えるかもしれません。私たちも時々メモリブロックを持っています。私を判断しないでください。

于 2014-08-28T14:09:47.323 に答える