2011-12-22 105 views
0

我有一个的DocumentViewer以下样式:如何从XAML ControlTemplate访问属性?

<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> 

我定义我的应用程序以下元素使用此样式:

<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> 

我的问题是,我怎么能找出什么“名称”属性的值来自XAML,所以我可以将它传递给ViewModel?

回答

1

假设你想通过的DocumentViewer的名称为CommandParameter到视图模型,最简单的就是使用

CommandParameter="{TemplateBinding Name}" 

将结合模板化控制到CommandParameter的名称。

+0

谢谢 - 完美的作品。我没有想到它会那么简单!其中一天,我会真正弄清楚这个XAML的东西(我希望)。 – PIntag 2011-12-22 15:56:35