在 WPF 中使用页面时更改窗口宽度

发布于 2024-08-09 01:39:55 字数 398 浏览 4 评论 0原文

我正在使用当前正在处理的 WPF 项目中的页面。但是我似乎无法弄清楚如何更改页面的宽度,或者更确切地说,更改托管页面的窗口的宽度?

设置页面宽度属性仅更改窗口框架内页面的宽度。

通过以下方式设置主窗口或导航窗口的 with:

<Application.MainWindow>
    <Window Width="400" />
</Application.MainWindow>

<Application.MainWindow>
    <NavigationWindow Width="400" />
</Application.MainWindow>

也不起作用。那么如何在XAML中设置窗口的宽度呢?

Im using pages in the WPF project that im currently working on. However i can't seem to figure out how to change the width of a page, or rather, the width of the window that hosts the pages?

Setting the page width property only changes the width of the page inside the window frame.

Setting the with of the mainwindow or navigationwindow through:

<Application.MainWindow>
    <Window Width="400" />
</Application.MainWindow>

<Application.MainWindow>
    <NavigationWindow Width="400" />
</Application.MainWindow>

Doesnt work either. So how do i set the width of the window in XAML?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水晶透心 2024-08-16 01:39:55

如果您只是询问如何设置窗口本身的尺寸,则只需打开窗口的 XAML 文件并设置宽度/高度属性:

Window Width="640" Height="480"

如果您确实想要差异。每页的窗口大小,您需要做更多的工作。页面的可用空间由主机窗口控制。页面没有内在的方式向主机请求更多的空间,但是您可以通过创建一些附加属性来构建您的 iwn 应用程序的支持,您的主机窗口知道这些属性并且可以由页面作者应用这些属性。当页面加载时,您的主机可以检查这些属性是否已设置并相应地调整其自身的宽度。

If you're just asking how to set the dimensions of the Window itself, then just open up the Window's XAML file and set the Width/Height properties:

Window Width="640" Height="480"

If you actually want a diff. Window size per Page, you need to do some more work. The available real-estate for a Page is controlled by the host Window. There is no intrinsic way for a Page to ask for more real estate from the host, but you could build support into your iwn app by creating some attached propertirs which your host window knows about and can be applied by the Page author. When the page loads your host can check to see if these properties are set and adjust its own width accordingly.

浅笑依然 2024-08-16 01:39:55

这确实是一个痛苦:你需要一个可以导航到页面的NavigationWindow。由于它继承自 Window,因此您可以设置此容器的高度和宽度。
-打开一个新的wpf应用程序
-删除您获得的标准window1。

因此更改App.xaml(删除StartupUri属性):

<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup">
<Application.Resources>

</Application.Resources>
</Application>

如下编写App.xaml.cs:

    public partial class App : Application
{
    private NavigationWindow navigationWindow;

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        navigationWindow = new NavigationWindow();
        navigationWindow.Height = 200;
        navigationWindow.Width = 100;
        var page = new Page1();
        navigationWindow.Navigate(page);
        navigationWindow.Show();
    }

您可以从项目菜单添加页面。这会给你类似的信息:

<Page x:Class="WpfApplication1.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Page1">
<Grid>
    <TextBlock>test</TextBlock>
</Grid>
</Page>

祝你好运!

It is indeed a pain: you need a NavigationWindow that can navigate to the page. As this inherits from Window you can set the Height and Width on this container.
-Open a new wpf aplication
-delete the standard window1 you get.

Change the App.xaml thus (delete the StartupUri attribute):

<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup">
<Application.Resources>

</Application.Resources>
</Application>

Write the App.xaml.cs thus:

    public partial class App : Application
{
    private NavigationWindow navigationWindow;

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        navigationWindow = new NavigationWindow();
        navigationWindow.Height = 200;
        navigationWindow.Width = 100;
        var page = new Page1();
        navigationWindow.Navigate(page);
        navigationWindow.Show();
    }

you can add a page from the project menu. This will give you something like:

<Page x:Class="WpfApplication1.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Page1">
<Grid>
    <TextBlock>test</TextBlock>
</Grid>
</Page>

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文