私は WPF を初めて使用しますが、このトピックに関する優れた本と、もちろん、このようなサイトの質の高い投稿のおかげで、短時間で多くの進歩を遂げることができました。しかし、今、それらの方法で理解できるように見えるものに出くわしたので、最初の質問を投稿します。
いくつかの UserControl ビューに適用するリソース ディクショナリに ControlTemplate があります。このテンプレートには、単純なオーバーレイ ボーダーと、[保存] と [キャンセル] の 2 つのボタンがあります。テンプレート化されたユーザー コントロールは、さまざまなテキスト ボックスなどを保持し、コンテキストに応じて一部の ViewModel にバインドされます。ビューで UserControl を使用/宣言するときに、コマンドを保存/キャンセル ボタンにバインドする方法を見つけようとしています。これは可能ですか、それとも私は何か非常に間違っていますか?
まず、テンプレート:
<ControlTemplate x:Key="OverlayEditorDialog"
TargetType="ContentControl">
<Grid>
<Border HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="DarkGray"
Opacity=".7"/>
<Border HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="DarkGray">
<Grid>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<ContentPresenter Grid.Row="0"/>
<Grid Grid.Row="1"
Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Column="1"
Content="Cancel"
***Command="{Binding CancelCommand}}"**
/>
<Button Grid.Column="0"
Content="Save"
***Command="{Binding Path=SaveCommand}"***/>
</Grid>
</Grid>
</Border>
</Grid>
</ControlTemplate>
テンプレートは、CustomerEditorOverlay ユーザー コントロールで使用されます。
<UserControl x:Class="GarazhApp.View.CustomerEditorOverlay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<UserControl.Resources>
<ResourceDictionary Source="Dictionary1.xaml"/>
</UserControl.Resources>
<ContentControl Template="{StaticResource ResourceKey=OverlayEditorDialog}">
<Grid Grid.Row="0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<SomeElement/>
<SomeOtherElement/>
</Grid>
</ContentControl>
...そして最後に、ユーザー コントロールは次のようにビューの一部として使用されます。
<local:CustomerEditorOverlay Visibility="{Binding Path=CustomerViewModel.ViewMode, Converter={StaticResource myConverter}, FallbackValue=Collapsed}"
d:IsHidden="True" />