WPF ウィンドウに単純な閉じるボタンを実装したいと考えています。ウィンドウは基本的に次のようになります。
<Window x:Class="MyApplication.MainWindow"
xmlns=""....
...."
Title="MainWindow" WindowState="Maximized" WindowStartupLocation="CenterScreen" x:Name="mainWindow">
<DockPanel LastChildFill="True">
<Ribbon DockPanel.Dock="Top">
<Ribbon.ApplicationMenu>
<RibbonApplicationMenu SmallImageSource="Resources/menu_16x16.png">
<RibbonApplicationMenu.FooterPaneContent>
<RibbonButton Label="Beenden"
Command="{Binding CmdCloseApp}"
CommandParameter="{Binding ElementName=mainWindow}"
SmallImageSource="Resources/ende_16x16.png"
HorizontalAlignment="Right"/>
</RibbonApplicationMenu.FooterPaneContent>
</RibbonApplicationMenu>
</Ribbon.ApplicationMenu>
</Ribbon>
</DockPanel>
</Window>
このウィンドウの DataContext は、分離コードで次のインスタンスに設定されます。MainWindowViewModel
MainWindowViewModel:
public class MainWindowViewModel
{
public ICommand CmdCloseApp { get; set; }
public MainWindowViewModel()
{
CmdCloseApp = new RelayCommand<Window>(CloseApp);
}
public void CloseApp(Window w)
{
w.Close();
}
}
In CloseApp
w は常に null です。文字列パラメーターなどを使用した他のすべてのコマンドは完全に機能します-私の唯一の問題は、ウィンドウ要素を取得できず、ビューモデルへの道が見つからないことです。
ご協力いただきありがとうございます!
編集
申し訳ありませんが、単純なボタンで試してみましたが、うまくいきました-問題は次の場合にのみ発生しますRibbonButton