1

我是 WPF 新手,我的问题是:

我有一个应用程序,其中有一个带有一些信息的圆形对象,我想拖放它。我的问题是当我在我的计算机上运行它时它工作正常,但是当我改变屏幕大小时,圆形对象形状会扭曲并变成椭圆。

我正在使用具有 5 行和具有相等比率(*)的列的网格。

当屏幕尺寸改变它的英寸(物理尺寸)长度时,它是什么东西!=宽度。

请给您的专家建议。

(Edit1:在 Canvas 中尝试过,在画布中的圆圈看起来像圆圈,而与任何屏幕无关,但想知道这是如何在网格中实现的!)

`

  <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="257*" />
        <RowDefinition Height="121*" />
        <RowDefinition Height="442*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="520*" />
        <ColumnDefinition Width="121*" />
        <ColumnDefinition Width="865*" />
    </Grid.ColumnDefinitions>
    <Ellipse Name="ellipse2" Stroke="Black" Grid.Row="1" Grid.Column="1" />   </Grid>

`

我知道我做错了,因为当分辨率变化比率变化时,会为不同的屏幕提供不同的物理单位。请建议使用网格的更好方法。

(Edit2:根据下面的本解决方案是结果比较,在我的例子中,我更喜欢示例 3,但必须以某种方式小心那个中风!!)

http://i.stack.imgur.com/1QpRR.jpg

4

2 回答 2

1

放大圆圈而不失真的另一种方法是除了视图框:'

<Grid.RowDefinitions>
    <RowDefinition Height="257*" />
    <RowDefinition Height="121*" />
    <RowDefinition Height="442*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="520*" />
    <ColumnDefinition Width="121*" />
    <ColumnDefinition Width="865*" />
</Grid.ColumnDefinitions>
    <Ellipse Stroke="Black" Grid.Row="1" Grid.Column="1" Stretch="Uniform"  />

'

与将圆圈放置在网格中间的 Viewbox 不同,此 Stretch = "Uniform" 拉伸圆圈并将其放置在网格的左侧。但所有屏幕的形状保持不变。它是基本的,我不知道我是怎么忘记的。

于 2013-10-23T11:33:25.413 回答
0

好吧,我不太确定您要做什么,所以这里有几个解决方案。他们都使用Grid,因为你特别要求它。但是,我不相信它真的合适。

您的问题是Ellipse大小由 决定Grid,并且没有强制纵横比的约束。因此,纵横比不仅会在屏幕分辨率不同时发生变化,还会在您调整窗口大小时发生变化。

解决方案1:设置椭圆的宽度和高度

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="257*" />
        <RowDefinition Height="121*" />
        <RowDefinition Height="442*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="520*" />
        <ColumnDefinition Width="121*" />
        <ColumnDefinition Width="865*" />
    </Grid.ColumnDefinitions>
    <Ellipse Stroke="Black" Grid.Row="1" Grid.Column="1" Width="20" Height="20" />
</Grid>

解决方案 2:在 Grid 中设置 Width 和 Height

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="257*" />
        <RowDefinition Height="20" />
        <RowDefinition Height="442*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="520*" />
        <ColumnDefinition Width="20" />
        <ColumnDefinition Width="865*" />
    </Grid.ColumnDefinitions>
    <Ellipse Stroke="Black" Grid.Row="1" Grid.Column="1" />
</Grid>

解决方案 3:使用 ViewBox

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="257*" />
        <RowDefinition Height="121*" />
        <RowDefinition Height="442*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="520*" />
        <ColumnDefinition Width="121*" />
        <ColumnDefinition Width="865*" />
    </Grid.ColumnDefinitions>
    <Viewbox Grid.Row="1" Grid.Column="1">
        <Ellipse Stroke="Black" Width="20" Height="20" />
    </Viewbox>
</Grid>

解决方案4:将椭圆的宽度和高度绑定在一起

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="257*" />
        <RowDefinition Height="121*" />
        <RowDefinition Height="442*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="520*" />
        <ColumnDefinition Width="121*" />
        <ColumnDefinition Width="865*" />
    </Grid.ColumnDefinitions>
    <Ellipse x:Name="ellipse" Stroke="Black" Grid.Row="1" Grid.Column="1" 
             Height="{Binding RelativeSource={RelativeSource Self}, Path=ActualWidth}" />
</Grid>

差异

解决方案 1 和 2,无论Grid大小是多少,都给圆一个恒定的大小。

解决方案 3 和 4,都根据大小调整圆的Grid大小。

解决方案 3 也会改变笔画的粗细,而解决方案 4 不会。

编辑:固定解决方案 4

于 2013-10-22T17:59:54.397 回答