【问题标题】:Viewbox and border视图框和边框
【发布时间】:2013-04-29 02:36:55
【问题描述】:

我正在使用 XAML 和 C# 编写一个商店应用程序。我想使用边框和视图框。我得到了边框样式,所以我不必多次设置属性。我将 BorderThickness 设置为 2,颜色设置为白色,但这会导致我的 Viewbox 出现问题。

它在 XAML 中:

<Viewbox Grid.Row="1" Stretch="Uniform">
  <Grid Width="600" Height="600">
    <Grid.Resources>
      <Style TargetType="Border">
        <Setter Property="BorderBrush" Value="White" />
        <Setter Property="BorderThickness" Value="2" />
      </Style>
      <Style TargetType="Grid">
        <Setter Property="VerticalAlignment" Value="Top" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="Width" Value="150" />
        <Setter Property="Height" Value="150" />
      </Style>
    </Grid.Resources>

    <StackPanel Orientation="Vertical">
      <StackPanel Orientation="Horizontal">
        <Grid>
          <Border>
            <Viewbox>
              <TextBlock Text="T" />
            </Viewbox>
          </Border>
        </Grid>

这样的结果是:

问题是字母“T”周围的缩放边框。

我不想删除 Grid.Resources 中 Border 的上述样式。到目前为止,我只找到了一种解决方案...

            <Viewbox>
              <Viewbox.Resources>
                <Style TargetType="Border">
                  <Setter Property="BorderBrush" Value="White" />
                  <Setter Property="BorderThickness" Value="0" />
                </Style>
              </Viewbox.Resources>
              <TextBlock Text="T" />

...什么会给出正确的结果:

,但我不想把这些行放在每个 ViewBoxes 之后,因为会有很多。 我还尝试制作一个组件,该组件的默认“资源”为零粗边框,但缩放比例很差。

所以我的问题是如何去除那个边框?

【问题讨论】:

    标签: xaml store


    【解决方案1】:

    您对 BorderThickness 使用零值是正确的。视觉树层次结构中可能还有另一个元素也包含导致这种情况的默认值。

    我现在无法对此进行测试,但我可以向您推荐这个工具: http://blois.us/Snoop/

    您可以通过将十字准线拖动到正在运行的调试应用程序上方来检查可视化树。每次我偶然发现这样的问题时,我发现查看哪些控件真正出现在运行时非常有用,因为使用 xaml 可能很难了解所有内容。希望你能找到它!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-29
      • 2016-06-25
      • 2011-03-16
      • 2021-12-29
      • 2011-07-28
      相关资源
      最近更新 更多