I have the following Style for a DocumentViewer:
<Style x:Key="MyDocumentViewerStyle" TargetType="{x:Type DocumentViewer}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DocumentViewer}">
<Border BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Focusable="False">
<Grid KeyboardNavigation.TabNavigation="Local">
<Grid.Background>
<SolidColorBrush Color="{DynamicResource ControlLightColor}" />
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Border Grid.Row="1" CornerRadius="2" HorizontalAlignment="Stretch" BorderThickness="0">
<ToolBar HorizontalAlignment="Center" Height="35"
ToolBarTray.IsLocked="True" KeyboardNavigation.TabNavigation="Continue" ToolBar.OverflowMode="Never">
<Button Command="ApplicationCommands.Print" Margin="10,0"
CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<!--Content="Print">-->
<Image Source="/ApniCureConsole;component/Images/Print.png" ToolTip="Print report" />
</Button>
<Separator />
<Button Command="NavigationCommands.IncreaseZoom" Margin="10,0"
CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<!--Content="Zoom In">-->
<Image Source="/ApniCureConsole;component/Images/MagnifyPlus.png" ToolTip="Zoom in" />
</Button>
<Button Command="NavigationCommands.DecreaseZoom" Margin="10,0"
CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<!--Content="Zoom Out" />-->
<Image Source="/ApniCureConsole;component/Images/MagnifyMinus.png" ToolTip="Zoom out" />
</Button>
<Separator />
<Button Margin="10,0,10,0"
Content="SAVE REPORT"
ToolTip="Save the report"
Style="{StaticResource CommandButton}"
Command="{Binding Path=SaveReportCommand}"
CommandParameter="{Binding ????}" <------WHAT DO I PUT HERE?
CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"/>
</ToolBar>
</Border>
<ScrollViewer Grid.Row="0" CanContentScroll="true" HorizontalScrollBarVisibility="Auto" x:Name="PART_ContentHost" IsTabStop="true">
<ScrollViewer.Background>
<LinearGradientBrush EndPoint="0,0" StartPoint="0,1">
<GradientStop Color="#559CB7A4" Offset="0" />
<GradientStop Color="White" Offset="1" />
</LinearGradientBrush>
</ScrollViewer.Background>
</ScrollViewer>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I’ve defined the following elements in my application that use this Style:
<TabControl Style="{StaticResource MyTabControlStyle}" TabStripPlacement="Top" >
<TabItem Style="{StaticResource MyTabItemStyle}" IsTabStop="False">
<TabItem.Header>
<TextBlock Text="REPORT ONE" Height="18" Padding="10, 0" FontSize="14" Style="{StaticResource MyTabItemText}"/>
</TabItem.Header>
<DocumentViewer Name="ReportOne" IsTabStop="False" Document="{Binding Report1}" ContextMenu="{x:Null}" Style="{StaticResource MyDocumentViewerStyle}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="0" BorderBrush="#FF00668E">
</DocumentViewer>
</TabItem>
<TabItem Style="{StaticResource MyTabItemStyle}" IsTabStop="False">
<TabItem.Header>
<TextBlock Text="REPORT TWO" Height="18" Padding="10, 0" FontSize="14" Style="{StaticResource MyTabItemText}"/>
</TabItem.Header>
<DocumentViewer Name="ReportTwo" IsTabStop="False" Document="{Binding Report2}" ContextMenu="{x:Null}" Style="{StaticResource MyDocumentViewerStyle}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="0" BorderBrush="#FF00668E">
</DocumentViewer>
</TabItem>
</TabControl>
My question is, how can I find out what the value for the “Name” property is from the XAML, so I can pass it my ViewModel?
Assuming that you want to pass the Name of the DocumentViewer as the CommandParameter to the ViewModel, the easiest is to use
which will bind the name of the templated control to the CommandParameter.