それはまた長いコーディングの日であり、私はおそらく何かを見過ごしています。しかし、私は逃したものを理解することはできません
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回行ったとき、それは私のソリューション全体の他のコントロールにあります
みんなありがとう。