【问题标题】:SizeToContent paints an unwanted borderSizeToContent 绘制不需要的边框
【发布时间】:2013-04-27 17:09:54
【问题描述】:

每当我尝试制作一个窗口并将SizeToContent 设置为WidthAndHeight 时,在打开窗口时会根据其内容正确调整大小,但它会在右侧和底部添加一个小边框。在调整大小时,这个问题消失了,当使用设置的高度和宽度时,这个问题也不会发生。

这是我的意思的一个示例:

您可以说这不是一个大问题,尽管我发现它使我的应用程序看起来不专业,尤其是当我需要展示它时。有谁知道为什么会这样,或者是否有解决方法?我正在用 C# 编写这个项目。

XAML 代码:

<Window x:Class="FPricing.InputDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="InputDialog" Width="400" Height="300" SizeToContent="WidthAndHeight">
    <StackPanel>
        <Label x:Name="question">?</Label>
        <TextBox x:Name="response"></TextBox>
        <Button Content="OK" IsDefault="True" Click="Button_Click" />
    </StackPanel>
</Window>

在创建类时传递值。

但是,即使没有自定义底层代码,我在创建的每个窗口都会遇到这个问题。

【问题讨论】:

  • 向我们展示您的开放活动代码,它可能是一个问题

标签: c# wpf sizetocontent


【解决方案1】:

通过结合 Gabriel 和 smg 答案设法解决了这个问题。 加载窗口后,获取有问题的边框,并将其 LayoutRounding 设置为 true。

this.Loaded += (sender, args) =>           
{
    var border = this.GetVisualChild(0) as Border;
    if (border != null)
        border.UseLayoutRounding = true;
};

【讨论】:

    【解决方案2】:

    &lt;Window UseLayoutRounding="True" /&gt; 为我工作。

    【讨论】:

    • 虽然此代码示例可能会回答这个问题,但最好在您的回答中包含一些基本解释。就目前而言,这个答案对未来的读者几乎没有价值。
    • @Gabriel 只对一个边框使用舍入,UseLayoutRounding 将舍入整个窗口。
    • 由于某种原因,这导致了一些标签被剪掉的问题(即缺少 ag 的底部),所以不幸的是它没有解决问题(但 BoJl4apa 的答案有效)。
    【解决方案3】:

    使用this tool(这很好,顺便说一句)我发现Window(它是直接子级)的Border控件并没有填满整个窗口,留下那个“边框”,它实际上是背景Window 控件的。

    我找到了解决方法。 Border 中的 WidthHeightNaN。如果将它们设置为整数值,“边框”就会消失。

    让我们使用ActualWidthActualHeight 的值,但四舍五入为整数。

    定义转换器:

    C#

    [ValueConversion(typeof(double), typeof(double))]
    public class RoundConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            return Math.Ceiling((double)value);
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return value;
        }
    }
    

    XAML(记住要包含您的命名空间,在本例中为“c”)

    <c:RoundConverter x:Key="RoundConverter"/>
    

    然后使用转换器创建一个将大小绑定到实际大小的样式。使用 Key 很重要,因此它不会应用于每个 Border(大多数控件都使用它):

    <Style TargetType="{x:Type Border}" x:Key="WindowBorder">
        <Setter Property="Width" Value="{Binding RelativeSource={RelativeSource Self}, Path=ActualWidth, Converter={StaticResource RoundConverter}}"/>
        <Setter Property="Height" Value="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight, Converter={StaticResource RoundConverter}}"/>
    </Style>
    

    最后,将此样式应用于窗口的第一个子窗口(@98​​7654335@ 控件):

    private void Window_Loaded(object sender, RoutedEventArgs e) {
        GetVisualChild(0).SetValue(StyleProperty, Application.Current.Resources["WindowBorder"]);
    }
    

    如果有人能以更简单的方式做到这一点,请也分享一下。

    【讨论】:

      【解决方案4】:

      好的,这是一个相关的答案,您可以在其中参考一个很好的答案。

      Automatic resizing when border content has changed

      所以基本上你想添加这样的东西,但把它放到你想要的值:

      <Border x:Name="border"
                  BorderBrush="Cornsilk"
                  BorderThickness="1">
              <Ellipse Width="40"
                       Height="20"
                       Fill="AliceBlue"
                       Stroke="Black" />
          </Border>
      

      【讨论】:

      • 谢谢你,添加这个会删除正确大小的边框,但不会删除底部的边框。然而,这对我来说不是一个大问题,因为这看起来并不算太糟糕。非常感谢您的快速响应!
      • 没问题,很高兴能帮上忙:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-30
      • 2018-05-13
      • 2010-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多