【问题标题】:Can I bind to use content from the controls nested in the templated parent?我可以绑定以使用嵌套在模板化父项中的控件的内容吗?
【发布时间】:2011-07-25 15:56:10
【问题描述】:

我想使用System.Windows.Controls.DocumentViewer 查看我使用TallComponents.PDF.Rasterizer 从PDF 文件创建的FixedDocument。这是可行的,但是,我想禁用或删除DocumentViewer 控件显示的一些工具栏按钮。我正在使用来自here 的模板示例。

<Style x:Key="{x:Type DocumentViewer}"
       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="Auto" />
              <RowDefinition Height="*" />
              <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <ToolBar ToolBarTray.IsLocked="True"
                     KeyboardNavigation.TabNavigation="Continue">
              <Button Command="NavigationCommands.IncreaseZoom"
  CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                      Content="Zoom In" />
              <Button Command="NavigationCommands.DecreaseZoom"
  CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                      Content="Zoom Out" />
              <Separator />
              <!--
              <Button Command="NavigationCommands.Zoom"
  CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                      CommandParameter="100.0"
                      Content="Actual Size" />
              -->
              <Button Command="DocumentViewer.FitToWidthCommand"
  CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                      Content="Fit to Width" />
              <Button Command="DocumentViewer.FitToMaxPagesAcrossCommand"
  CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                      CommandParameter="1"
                      Content="Whole Page" />
              <Button Command="DocumentViewer.FitToMaxPagesAcrossCommand"
  CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                      CommandParameter="2"
                      Content="Two Pages" />
            </ToolBar>
            <ScrollViewer Grid.Row="1"
                          CanContentScroll="true"
                          HorizontalScrollBarVisibility="Auto"
                          x:Name="PART_ContentHost"
                          IsTabStop="true">
              <ScrollViewer.Background>
                <LinearGradientBrush EndPoint="0.5,1"
                                     StartPoint="0.5,0">
                  <GradientStop Color="{DynamicResource ControlLightColor}"
                                Offset="0" />
                  <GradientStop Color="{DynamicResource ControlMediumColor}"
                                Offset="1" />
                </LinearGradientBrush>
              </ScrollViewer.Background>
            </ScrollViewer>
            <ContentControl Grid.Row="2"
                            x:Name="PART_FindToolBarHost"/>
          </Grid>
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

这与链接的示例非常相似,但我删除了打印和复制命令按钮。我不希望用户能够使用这些按钮。

我还不得不注释掉 Zoom 命令,IDE 抱怨这不是一个有效的命令,该问题的答案的奖金投票。

这一切都有效,但在模板化之后,我不再获得工具栏按钮上的图像,只有我在 Content 中指定的文本。所以我的问题是,“我可以绑定以使用嵌套在模板父项中的控件的内容吗?”

【问题讨论】:

    标签: wpf templates binding controltemplate documentviewer


    【解决方案1】:

    这是我使用 Blend 为 DocumentViewer 提取和编辑模板时得到的结果。我注释掉了你提到的两个按钮。

    就 Zoom 命令而言,它可能会抱怨命令无效,因为您缺少 System.Windows.Documents 的命名空间导入。

    Xaml:

    <Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:System_Windows_Documents="clr-namespace:System.Windows.Documents;assembly=PresentationUI"
    x:Class="WpfApplication6.Page2"
    x:Name="Page"
    WindowTitle="Page"
    FlowDirection="LeftToRight"
    Width="640" Height="480"
    WindowWidth="640" WindowHeight="480">
    
    <Page.Resources>
        <Style x:Key="DocumentViewerStyle1" BasedOn="{x:Null}" 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="ContextMenu" Value="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerContextMenu, TypeInTargetAssembly={x:Type System_Windows_Documents:PresentationUIStyleResources}}}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DocumentViewer}">
                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Focusable="False">
                            <Grid Background="{TemplateBinding Background}" KeyboardNavigation.TabNavigation="Local">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <ContentControl Grid.Column="0" Focusable="{TemplateBinding Focusable}" Grid.Row="0" Style="{DynamicResource ContentControlStyle1}" TabIndex="0"/>
                                <ScrollViewer x:Name="PART_ContentHost" CanContentScroll="true" Grid.Column="0" Focusable="{TemplateBinding Focusable}" HorizontalScrollBarVisibility="Auto" IsTabStop="true" Grid.Row="1" TabIndex="1"/>
                                <DockPanel Grid.Row="1">
                                    <FrameworkElement DockPanel.Dock="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"/>
                                    <Rectangle Height="10" Visibility="Visible" VerticalAlignment="top">
                                        <Rectangle.Fill>
                                            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                                <LinearGradientBrush.GradientStops>
                                                    <GradientStopCollection>
                                                        <GradientStop Color="#66000000" Offset="0"/>
                                                        <GradientStop Color="Transparent" Offset="1"/>
                                                    </GradientStopCollection>
                                                </LinearGradientBrush.GradientStops>
                                            </LinearGradientBrush>
                                        </Rectangle.Fill>
                                    </Rectangle>
                                </DockPanel>
                                <ContentControl x:Name="PART_FindToolBarHost" Grid.Column="0" Focusable="{TemplateBinding Focusable}" Grid.Row="2" TabIndex="2"/>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="ContentControlStyle1" TargetType="{x:Type ContentControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ContentControl}">
                        <ToolBar Focusable="{TemplateBinding Focusable}" ToolBarTray.IsLocked="True" Language="en-us" KeyboardNavigation.TabNavigation="Continue" Uid="ToolBar_2">
                            <!-- <Button x:Name="PrintButton" Background="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerPrintButton, TypeInTargetAssembly={x:Type System_Windows_Documents:PresentationUIStyleResources}}}" CommandTarget="{Binding TemplatedParent, RelativeSource={RelativeSource TemplatedParent}}" Command="ApplicationCommands.Print" IsTabStop="True" Margin="2" Padding="2" Style="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerButtonStyle, TypeInTargetAssembly={x:Type System_Windows_Documents:PresentationUIStyleResources}}}" ToolTipService.ShowOnDisabled="True" TabIndex="0" ToolTip="Print (Ctrl+P)" Uid="Button_14" VerticalAlignment="Center" Width="24"/> -->
                            <!-- <Button x:Name="CopyButton" Background="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerCopyButton, TypeInTargetAssembly={x:Type System_Windows_Documents:PresentationUIStyleResources}}}" CommandTarget="{Binding TemplatedParent, RelativeSource={RelativeSource TemplatedParent}}" Command="ApplicationCommands.Copy" IsTabStop="True" Margin="2" Padding="2" Style="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerButtonStyle, TypeInTargetAssembly={x:Type System_Windows_Documents:PresentationUIStyleResources}}}" ToolTipService.ShowOnDisabled="True" TabIndex="1" ToolTip="Copy (Ctrl+C)" Uid="Button_15" VerticalAlignment="Center" Width="24"/> -->
                            <!-- <Separator Uid="Separator_110"/> -->
                            <Button x:Name="ZoomInButton" Background="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerZoomInButton, TypeInTargetAssembly={x:Type System_Windows_Documents:PresentationUIStyleResources}}}" CommandTarget="{Binding TemplatedParent, RelativeSource={RelativeSource TemplatedParent}}" Command="NavigationCommands.IncreaseZoom" IsTabStop="True" Margin="2" Padding="2" Style="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerButtonStyle, TypeInTargetAssembly={x:Type System_Windows_Documents:PresentationUIStyleResources}}}" ToolTipService.ShowOnDisabled="True" TabIndex="3" ToolTip="Increase the size of the content (Ctrl +)" Uid="Button_16" VerticalAlignment="Center" Width="24"/>
                            <Button x:Name="ZoomOutButton" Background="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerZoomOutButton, TypeInTargetAssembly={x:Type System_Windows_Documents:PresentationUIStyleResources}}}" CommandTarget="{Binding TemplatedParent, RelativeSource={RelativeSource TemplatedParent}}" Command="NavigationCommands.DecreaseZoom" IsTabStop="True" Margin="2" Padding="2" Style="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerButtonStyle, TypeInTargetAssembly={x:Type System_Windows_Documents:PresentationUIStyleResources}}}" ToolTipService.ShowOnDisabled="True" TabIndex="4" ToolTip="Decrease the size of the content (Ctrl -)" Uid="Button_17" VerticalAlignment="Center" Width="24"/>
                            <Separator Uid="Separator_111"/>
                            <Button x:Name="ActualSizeButton" Background="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerActualSizeButton, TypeInTargetAssembly={x:Type System_Windows_Documents:PresentationUIStyleResources}}}" CommandTarget="{Binding TemplatedParent, RelativeSource={RelativeSource TemplatedParent}}" CommandParameter="100.0" Command="NavigationCommands.Zoom" IsTabStop="True" Margin="2" Padding="2" Style="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerButtonStyle, TypeInTargetAssembly={x:Type System_Windows_Documents:PresentationUIStyleResources}}}" ToolTipService.ShowOnDisabled="True" TabIndex="5" ToolTip="100% (Ctrl+1)" Uid="Button_18" VerticalAlignment="Center" Width="24"/>
                            <Button x:Name="PageWidthButton" Background="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerPageWidthButton, TypeInTargetAssembly={x:Type System_Windows_Documents:PresentationUIStyleResources}}}" CommandTarget="{Binding TemplatedParent, RelativeSource={RelativeSource TemplatedParent}}" Command="DocumentViewer.FitToWidthCommand" IsTabStop="True" Margin="2" Padding="2" Style="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerButtonStyle, TypeInTargetAssembly={x:Type System_Windows_Documents:PresentationUIStyleResources}}}" ToolTipService.ShowOnDisabled="True" TabIndex="6" ToolTip="Page Width (Ctrl+2)" Uid="Button_19" VerticalAlignment="Center" Width="24"/>
                            <Button x:Name="WholePageButton" Background="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerWholePageButton, TypeInTargetAssembly={x:Type System_Windows_Documents:PresentationUIStyleResources}}}" CommandTarget="{Binding TemplatedParent, RelativeSource={RelativeSource TemplatedParent}}" CommandParameter="1" Command="DocumentViewer.FitToMaxPagesAcrossCommand" IsTabStop="True" Margin="2" Padding="2" Style="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerButtonStyle, TypeInTargetAssembly={x:Type System_Windows_Documents:PresentationUIStyleResources}}}" ToolTipService.ShowOnDisabled="True" TabIndex="7" ToolTip="Whole Page (Ctrl+3)" Uid="Button_20" VerticalAlignment="Center" Width="24"/>
                            <Button x:Name="TwoPagesButton" Background="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerTwoPagesButton, TypeInTargetAssembly={x:Type System_Windows_Documents:PresentationUIStyleResources}}}" CommandTarget="{Binding TemplatedParent, RelativeSource={RelativeSource TemplatedParent}}" CommandParameter="2" Command="DocumentViewer.FitToMaxPagesAcrossCommand" IsTabStop="True" Margin="2" Padding="2" Style="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerButtonStyle, TypeInTargetAssembly={x:Type System_Windows_Documents:PresentationUIStyleResources}}}" ToolTipService.ShowOnDisabled="True" TabIndex="8" ToolTip="Two Pages (Ctrl+4)" Uid="Button_21" VerticalAlignment="Center" Width="24"/>
                        </ToolBar>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Page.Resources>
    
    <Grid x:Name="LayoutRoot">
        <DocumentViewer Style="{DynamicResource DocumentViewerStyle1}"/>
    </Grid>
    

    【讨论】:

    • 好极了,我可以确认这是可行的。我必须通过在文件系统中导航到它来添加对 PresentationUI 的引用,在我的框架目录的 WPF 子文件夹中。我想我从中学到的主要东西是,如果你想用 WPF 做任何复杂的事情,请使用 Blend。如果您希望新控件看起来简单,似乎控件模板只是“简单”。
    • 缩放命令似乎只是设计时的问题!?!
    • 很高兴它成功了!我同意 Blend 让对模板进行复杂更改变得更加简单!我主要在 VS 中使用 XAML 视图,但转而混合这种东西。至于缩放命令,我不确定它是否可以在没有参考的情况下在运行时工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 2017-03-12
    • 1970-01-01
    相关资源
    最近更新 更多