0

我在 WPF TreeView 控件旁边有一个 WPF WebBrowser 控件。两者都有默认样式和默认属性。除了将它们放在我的 WPF 窗口中之外,我没有更改任何内容。如果您查看下面的屏幕截图,您可以看到这两个控件有两个不同的滚动条。我不知道为什么会这样。

有没有办法让一个控件的滚动条看起来像另一个,或者只是将它们都默认为 windows 样式?

谢谢

在此处输入图像描述

4

3 回答 3

1

要回答您的评论:

有没有办法将该主题保留在我的资源中(我的功能区控件使用它)并从该树视图控件中删除滚动条主题?– Landin Martens 6 月 23 日 19:49

是的。

选项1

您可以像这样覆盖控件的默认样式:

<Style TargetType="{x:Type TreeView}">
  ...
</Style>

这将更改每个 TreeView 控件的样式(假设样式是在 App.xaml 字典中定义的)。如果只想覆盖少数控件的样式,可以命名:

<Style x:Key="XTreeViewStyle"
       TargetType="{x:Type TreeView}">
  ...
</Style>

并为这些控件显式设置样式 ( <TreeView Style="{StaticResource XTreeViewStyle}"/>)。或者,您可以在特定上下文中创建样式,例如 Grid(因此仅影响 Grid 中的那些控件):

<Grid>
  <Grid.Resources>
    <Style TargetType="{x:Type TreeView}">
    </Style>
  </Grid.Resources>
</Grid>

我的猜测是,覆盖滚动条的样式并不是专门针对 TreeViews 的……它可能针对的是 TreeView 继承自的控件。因此,请寻找针对 TreeView、ItemsControl、Control、FrameworkElement 或 UIElement 的默认样式。当你找到它时,你可以给它一个名字,或者将样式移动到一个更有限的上下文中。

例如,您的样式可能设置如下:

<Style TargetType="{x:Type ItemsControl}">
  ...
</Style>

<Style TargetType="{x:Type Ribbon}">
  <Style.Template>
    <ControlTemplate>
      <ItemsControl>
        ...
      </ItemsControl>
    </ControlTemplate>
  </Style.Template>
</Style>

要让 ItemsControl 样式只影响 Ribbon,您可以将 ItemsControl 样式移到 Ribbon 的模板中:

<Style TargetType="{x:Type Ribbon}">
  <Style.Template>
    <ControlTemplate>
      <ControlTemplate.Resources>
        <Style TargetType="{x:Type ItemsControl}">
          ...
        </Style>
      </ControlTemplate.Resources>
      <ItemsControl>
        ...
      </ItemsControl>
    </ControlTemplate>
  </Style.Template>
</Style>

然而,确切的方法将取决于您的样式设置的准确程度,这可能比我建议的要复杂得多。例如,如果您有几种不同的样式都需要使用该 ItemsControl 样式,则可以像这样使用命名和继承:

<Style TargetType="{x:Type ItemsControl}"
       x:Key="XItemsControlStyle">
  ...
</Style>

<Style TargetType="{x:Type Ribbon}">
  <Style.Template>
    <ControlTemplate>
      <ControlTemplate.Resources>
        <Style TargetType="{x:Type ItemsControl}"
               BasedOn="{StaticResource XItemsControlStyle}">
          ...
        </Style>
      </ControlTemplate.Resources>
      <ItemsControl>
        ...
      </ItemsControl>
    </ControlTemplate>
  </Style.Template>
</Style>

<Style TargetType="{x:Type RibbonItem}">
  <Style.Template>
    <ControlTemplate>
      <ControlTemplate.Resources>
        <Style TargetType="{x:Type ItemsControl}"
               BasedOn="{StaticResource XItemsControlStyle}">
          ...
        </Style>
      </ControlTemplate.Resources>
      <ItemsControl>
        ...
      </ItemsControl>
    </ControlTemplate>
  </Style.Template>
</Style>

选项 2

显式设置 TreeView 的样式。这将意味着确定 TreeView 的原始样式(或至少是您想要使用的样式),为其命名,然后在您想要设置样式的 TreeView 上显式设置。(有关如何执行此操作,请参见选项 2)

我相信这些链接将为您提供默认样式:

于 2012-07-02T19:56:04.450 回答
0

WebBrowser只是原生组件的包装器,在 WPF 中很难处理它。我在项目中遇到了很多痛苦。

因此,从 CodePlex尝试Chromium for WPF可能是个好主意。它更加灵活。

于 2012-06-23T20:05:09.013 回答
0

看起来您的应用程序资源中有一个自定义主题。WebBrowser不使用主题中的样式,因为它不是 WPF 控件,它只是 Win32 控件的包装器。所以你不能让 WebBrowser 滚动条看起来像左边的那个。如果您想摆脱自定义样式,则应将其从资源中删除。

于 2012-06-23T19:36:14.150 回答