如何从XAML ControlTemplate访问属性?

时间:2011-12-22 02:03:06

标签: wpf xaml

我有一个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>

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

<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中找出“Name”属性的值是什么,所以我可以将它传递给我的ViewModel?

1 个答案:

答案 0 :(得分:1)

假设您要将DocumentViewer的Name作为CommandParameter传递给ViewModel,最简单的方法是使用

CommandParameter="{TemplateBinding Name}"

将模板化控件的名称绑定到CommandParameter。