内部链接使用后,WPF DocumentViewer失去了自定义样式 [英] WPF DocumentViewer loses custom style after internal link use

查看:253
本文介绍了内部链接使用后,WPF DocumentViewer失去了自定义样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想构建一个小型应用程序,该应用程序可以浏览文件系统并显示多个文档.我想显示的一种文档类型是xps. DocumentViewer做得很好.与框架结合使用时,查看器可以处理内部链接(包含在xps文档中.).对于我的应用程序,我构建了一个自定义工具栏(zoom,page,fitsize ...),以便为每种类型的文档提供一个工具栏.所以我需要删除documentViewer的工具栏.下面是代码.

Hi want to build a small application, that allows to navigate through filesystem and displays several documents. One type of document i want to show, is xps. DocumentViewer is doing well. In combination with a Frame the viewer can handle internal links (included in the xps documents.). For my Application i build a custom toolbar (zoom, page, fitsize ...), to have a one toolbar for every kind of document. So i needed to remove the toolbar of the documentViewer. Below is the code.

<Style x:Key="{x:Type DocumentViewer}"
   TargetType="{x:Type DocumentViewer}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DocumentViewer}">
                <Border BorderThickness="{TemplateBinding BorderThickness}"
            BorderBrush="{TemplateBinding BorderBrush}"
            Focusable="False">
                    <ScrollViewer 
                      CanContentScroll="true"
                      HorizontalScrollBarVisibility="Auto"
                      x:Name="PART_ContentHost"
                      IsTabStop="true">
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

那很好,但是在激活xps中的链接后,DocumentViewer工具栏再次出现.如何避免这种情况?

That works fine, but after activating a link in the xps, the DocumentViewer Toolbar appears again. How to avoid that?

推荐答案

问题是导航服务首次单击链接后会创建新的标准DocumentViewer.即使在XAML中使用从DocumentViewer派生的组件,也会发生这种情况.

The problem is that the navigation service creates a new standard DocumentViewer after clicking on a link for the first time. This happens even when you use a component derived from DocumentViewer in your XAML.

您可以通过在导航容器的LayoutUpdated事件

You can work around this issue by manually resetting the style in your navigation container's LayoutUpdated event

XAML

<Frame LayoutUpdated="OnFrameLayoutUpdated">
    <Frame.Content>
        <DocumentViewer ... />
    </Frame.Content>
</Frame>

背后的代码

private void OnFrameLayoutUpdated(object sender, EventArgs e)
{
    var viewer = GetFirstChildByType<DocumentViewer>(this);
    if (viewer == null) return;
    viewer.Style = (Style) FindResource("DocumentViewerStyle");
}

private T GetFirstChildByType<T>(DependencyObject prop) where T : DependencyObject
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(prop); i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild((prop), i) as DependencyObject;
        if (child == null)
            continue;

        T castedProp = child as T;
        if (castedProp != null)
            return castedProp;

        castedProp = GetFirstChildByType<T>(child);

        if (castedProp != null)
            return castedProp;
    }
    return null;
}

这篇关于内部链接使用后,WPF DocumentViewer失去了自定义样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆