动态调整页面和属性 C# Visual Studio 2015
Dynamically resize page and attributes C# Visual Studio 2015
我想在Visual Studio 2015 年做一个应用程序。我是一个新手,所以我知道的不多。正如标题所暗示的那样,我无法将按钮和页面项目调整为框架的大小。这是我的 XAML 代码:
<Page
x:Class="Rodojo_start.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Rodojo_start"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<RelativePanel>
<Button Width="320" Height="45" Name="Manage_btn" Click="Manage_btn_Click" FontFamily="Segoe MDL2 Assets" Content="" Margin="0,0,0,0"></Button>
</RelativePanel>
<RelativePanel>
<Button Width="320" Height="45" Name="Sale_btn" Click="Sale_btn_Click" FontFamily="Segoe MDL2 Assets" Content="" Margin="320,0,0,0"></Button>
</RelativePanel>
<RelativePanel>
<Button Width="320" Height="45" Name="settingsbtn" Click="settingsbtn_Click" FontFamily="Segoe MDL2 Assets" Content="" Margin="640,0,0,0"></Button>
</RelativePanel>
<RelativePanel>
<Button Width="320" Height="45" Name="aboutbtn" Click="aboutbtn_Click" FontFamily="Segoe MDL2 Assets" Content="" Margin="960,0,0,0"></Button>
</RelativePanel>
</Grid>
现在,按钮都是 320 像素宽。我希望它们自行调整大小,但仍将它们保持在屏幕顶部。我尝试了 "ViewBox" 部分。这使它调整大小,但将按钮放在屏幕中间。
如有任何帮助,我们将不胜感激。
谢谢。
我个人认为ViewBox的解决方案比较混乱。很难理解控件将如何调整大小。
在 WPF 中,Grid、DockPanel 和 UniformGrid 可以自动调整其内容的大小。您已经开始使用 Grid 来定义您的布局..好吧,继续吧!为了让网格调整其内容的大小,这将取决于您如何设置其行和列的高度和宽度。
你的情况:
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
将高度或宽度设置为 "Auto"
,网格将根据其内容的大小调整。将其设置为 "*"
,网格的大小将调整为可用的 space 大小。设置一些具有特定高度的行,如 "45"
,无论如何都会将行保持在 45。
您还可以使用 MinHeight/MaxHeight
和 MinWidth/MaxWidth
来定义行和列的大小,这将调整网格的大小,但要考虑最大或最小尺寸。
Therefore, my suggestion to you: Remove the RelativePanels, and define
a layout grid. If you want to automatically resize the contents, don't
define specific sizes, but use "Auto", "*", MaxHeight, MinHeight, MaxWidth, MinWidth
instead. Even your buttons. If you set your button
to have a Width of 45, your button will keep a width of 45, when
resized!
希望我有所帮助,这是另一个来源:How to make all controls resize accordingly proportionally when window is maximized?
好的,我从上面的一切都明白了。这是我的结束代码:
<Page
x:Class="Rodojo_start.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Rodojo_start"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Name="Manage_btn" Click="Manage_btn_Click" FontFamily="Segoe MDL2 Assets" Content="" Margin="0,0,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<Button Name="Sale_btn" Click="Sale_btn_Click" FontFamily="Segoe MDL2 Assets" Content="" Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="1"/>
<Button Name="settingsbtn" Click="settingsbtn_Click" FontFamily="Segoe MDL2 Assets" Content="" Margin="0" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<Button Name="aboutbtn" Click="aboutbtn_Click" FontFamily="Segoe MDL2 Assets" Content="" Margin="0" Grid.Column="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Grid>
</Page>
它完全按照我想要的方式工作。再次感谢大家所做的一切!
我想在Visual Studio 2015 年做一个应用程序。我是一个新手,所以我知道的不多。正如标题所暗示的那样,我无法将按钮和页面项目调整为框架的大小。这是我的 XAML 代码:
<Page
x:Class="Rodojo_start.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Rodojo_start"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<RelativePanel>
<Button Width="320" Height="45" Name="Manage_btn" Click="Manage_btn_Click" FontFamily="Segoe MDL2 Assets" Content="" Margin="0,0,0,0"></Button>
</RelativePanel>
<RelativePanel>
<Button Width="320" Height="45" Name="Sale_btn" Click="Sale_btn_Click" FontFamily="Segoe MDL2 Assets" Content="" Margin="320,0,0,0"></Button>
</RelativePanel>
<RelativePanel>
<Button Width="320" Height="45" Name="settingsbtn" Click="settingsbtn_Click" FontFamily="Segoe MDL2 Assets" Content="" Margin="640,0,0,0"></Button>
</RelativePanel>
<RelativePanel>
<Button Width="320" Height="45" Name="aboutbtn" Click="aboutbtn_Click" FontFamily="Segoe MDL2 Assets" Content="" Margin="960,0,0,0"></Button>
</RelativePanel>
</Grid>
现在,按钮都是 320 像素宽。我希望它们自行调整大小,但仍将它们保持在屏幕顶部。我尝试了 "ViewBox" 部分。这使它调整大小,但将按钮放在屏幕中间。
如有任何帮助,我们将不胜感激。
谢谢。
我个人认为ViewBox的解决方案比较混乱。很难理解控件将如何调整大小。
在 WPF 中,Grid、DockPanel 和 UniformGrid 可以自动调整其内容的大小。您已经开始使用 Grid 来定义您的布局..好吧,继续吧!为了让网格调整其内容的大小,这将取决于您如何设置其行和列的高度和宽度。
你的情况:
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
将高度或宽度设置为 "Auto"
,网格将根据其内容的大小调整。将其设置为 "*"
,网格的大小将调整为可用的 space 大小。设置一些具有特定高度的行,如 "45"
,无论如何都会将行保持在 45。
您还可以使用 MinHeight/MaxHeight
和 MinWidth/MaxWidth
来定义行和列的大小,这将调整网格的大小,但要考虑最大或最小尺寸。
Therefore, my suggestion to you: Remove the RelativePanels, and define a layout grid. If you want to automatically resize the contents, don't define specific sizes, but use
"Auto", "*", MaxHeight, MinHeight, MaxWidth, MinWidth
instead. Even your buttons. If you set your button to have a Width of 45, your button will keep a width of 45, when resized!
希望我有所帮助,这是另一个来源:How to make all controls resize accordingly proportionally when window is maximized?
好的,我从上面的一切都明白了。这是我的结束代码:
<Page
x:Class="Rodojo_start.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Rodojo_start"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Name="Manage_btn" Click="Manage_btn_Click" FontFamily="Segoe MDL2 Assets" Content="" Margin="0,0,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<Button Name="Sale_btn" Click="Sale_btn_Click" FontFamily="Segoe MDL2 Assets" Content="" Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="1"/>
<Button Name="settingsbtn" Click="settingsbtn_Click" FontFamily="Segoe MDL2 Assets" Content="" Margin="0" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<Button Name="aboutbtn" Click="aboutbtn_Click" FontFamily="Segoe MDL2 Assets" Content="" Margin="0" Grid.Column="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Grid>
</Page>
它完全按照我想要的方式工作。再次感谢大家所做的一切!