简体   繁体   English

如何以编程方式保持 wpf 中对象的纵横比

[英]How to programmatically maintain aspect ratio of object in wpf

I programmatically add a Border with a certain width and height to a grid.我以编程方式将具有特定宽度和高度的Border添加到网格中。 However, I want to get either one of the following:但是,我想获得以下任一项:

  1. Make the border keep aspect ratio and fill make it as big as possible inside the grid使边框保持纵横比并填充使其在网格内尽可能大
  2. Make the border scale whenever the grid scales down or up (so not particularily the biggest possible, more like a percentage of the grid)每当网格缩小或放大时都进行边框缩放(因此不是特别大,更像是网格的百分比)

At the moment this is the situation when I resize my window:目前这是我调整窗口大小时的情况:

Color borderColor = (Color)ColorConverter.ConvertFromString(BorderColor);
Color backgroundColor = (Color)ColorConverter.ConvertFromString(BackgroundColor);

Border border = new Border();
border.BorderThickness = new Thickness(BorderSize);
border.CornerRadius = new CornerRadius(TopLeftCornerRadius, TopRightCornerRadius, BottomRightCornerRadius, BottomLeftCornerRadius);
border.BorderBrush = (SolidColorBrush)(new BrushConverter().ConvertFrom(BorderColor));
border.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom(BackgroundColor));

border.Width = Width;
border.Height = Height;

border.Margin = new Thickness(10);

previewgrid.Children.Add(border);

The normal situation:正常情况:

在此处输入图片说明

The scaled situation:规模化情况:

在此处输入图片说明

So I would like it to resize properly and stay inside the white rectangle.所以我希望它正确调整大小并留在白色矩形内。 By the way, the white grid has a margin as you can see ;-) Thanks in advance!顺便说一下,正如您所看到的,白色网格有一个边距 ;-) 提前致谢!

As lerthe61 suggested, just use a Viewbox with its Stretch property set to Uniform :正如 lerthe61 建议的那样,只需使用一个ViewboxStretch属性设置为Uniform

Color borderColor = (Color)ColorConverter.ConvertFromString(BorderColor);
Color backgroundColor = (Color)ColorConverter.ConvertFromString(BackgroundColor);

Border border = new Border();
border.BorderThickness = new Thickness(BorderSize);
border.CornerRadius = new CornerRadius(TopLeftCornerRadius, TopRightCornerRadius, BottomRightCornerRadius, BottomLeftCornerRadius);
border.BorderBrush = (SolidColorBrush)(new BrushConverter().ConvertFrom(BorderColor));
border.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom(BackgroundColor));

border.Width = Width;
border.Height = Height;

border.Margin = new Thickness(10);

Viewbox viewBox = new Viewbox();
viewBox.Stretch = Stretch.Uniform;
viewBox.Child = border;

previewgrid.Children.Add(viewBox);

Please, note that this solution does not work if previewgrid is a Canvas .请注意,如果previewgridCanvas ,则此解决方案不起作用。 I hope it can help you.我希望它能帮助你。

The simplest way:最简单的方法:

 <Grid Margin="50">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="50" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="50" />
    </Grid.ColumnDefinitions>
    <Border CornerRadius="50,0,0,50"
            Background="Green" />
    <Border CornerRadius="0"
            Grid.Column="1"
            Background="Green" />
    <Border CornerRadius="0,50,50,0"
            Grid.Column="2"
            Background="Green" />

</Grid>

By C#:通过 C#:

        myGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(50) });
        myGrid.ColumnDefinitions.Add(new ColumnDefinition());
        myGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(50) });

        Border b1 = new Border
        {
            Background = new SolidColorBrush(Colors.Blue),
            CornerRadius = new CornerRadius(100, 0, 0, 100)
        };

        Grid.SetColumn(b1, 0);

        Border b2 = new Border
        {
            Background = new SolidColorBrush(Colors.Blue),
        };
        Grid.SetColumn(b2, 1);
        Border b3 = new Border
        {
            Background = new SolidColorBrush(Colors.Blue),
            CornerRadius = new CornerRadius(0, 100, 100, 0),
        };
        Grid.SetColumn(b3, 2);

        myGrid.Children.Add(b1);
        myGrid.Children.Add(b2);
        myGrid.Children.Add(b3);

Normal:普通的:

在此处输入图片说明 Resized:调整大小:

在此处输入图片说明

Is it good enough for you?对你来说足够好吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何在 Unity c# 中保持纵横比并将 3D 对象设置为在所选纵横比的纵横比内缩放? - How should I Maintain aspect ratio in Unity c# and set the 3D object to scale within the aspect ratio of the selected aspect ratio? 在这种情况下如何保持网格的纵横比? - How to maintain the aspect ratio of Grid in this case? 如何调整图像大小并保持宽高比? - How to resize my image and maintain the aspect ratio? 调整Window Forms应用程序大小时如何保持长宽比? - How to maintain the aspect ratio when resizing a Window Forms application? Unity)我怎样才能保持精灵的纵横比? - Unity) How can I maintain sprites aspect ratio? 使用2个NumericUpDown字段来维护Dimensions纵横比 - Using 2 NumericUpDown fields to maintain Dimensions aspect ratio 如何在运行时调整网格上的WPF控件(保持纵横比) - How to resize WPF controls on a grid at runtime (keeping aspect ratio) 如何在 WPF 中保持可缩放、可滚动内容的纵横比? - How do I keep aspect ratio on scalable, scrollable content in WPF? Win Form调整大小时如何保持Datagridview列宽的宽高比 - How to maintain aspect ratio of datagridview column width when win form resize 如何在Textblock中缩小字体大小以适合内容的宽度并保持字体长宽比 - How to shrink font size in Textblock to fit width of content AND maintain font aspect ratio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM