简体   繁体   English

如何将WPF窗口嵌入另一个窗口并通过XAML或代码调整其大小

[英]How to embed WPF Window into another Window and adjust its size via XAML or code

First of all it's my first day using Xaml so this question might be dummy for you, but i totally got lost. 首先,这是我使用Xaml的第一天所以这个问题对你来说可能是假的,但我完全迷路了。

Overview 概观

My technique is that i have MainWindow.xaml and it's split into three areas (using grid columns) the columns width being set automatically. 我的技术是我有MainWindow.xaml,它被分成三个区域(使用网格列)自动设置列宽。

Based on some actions in the right column, the middle column with show a page let's say Page.xaml that exists in different namespace. 基于右列中的某些操作,显示页面的中间列让我们说不同命名空间中存在的Page.xaml。

What i'm seeking for 我正在寻找什么

The problem is i need to set the width and height for this page to be equal the middle column width and height as it will fit this area. 问题是我需要将此页面的宽度和高度设置为等于中间列宽度和高度,因为它将适合此区域。

Notes 笔记

I have very small experience with xaml and binding techniques. 我对xaml和绑定技术的经验很少。

MainWindow.Xaml MainWindow.Xaml

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" 
    WindowState="Maximized"
    ResizeMode="NoResize"
    WindowStartupLocation="CenterScreen"
    Title="MainWindow" d:DesignWidth="1366" d:DesignHeight="768">

<Grid x:Name="MainGrid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1.2*" x:Name="LeftColoumn" />
        <ColumnDefinition Width="3*" x:Name="CenterColoumn" />
        <ColumnDefinition Width=".8*" x:Name="RightColoumn" />
    </Grid.ColumnDefinitions>

    <ScrollViewer Grid.Column="2">
        <StackPanel Orientation="Vertical" x:Name="RightStackPanel" Background="LightGray" >
            <Border BorderBrush="{x:Null}" Height="50" >
                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap" FontWeight="SemiBold" FontStyle="Normal" Margin="3" FontSize="20" >Others</TextBlock>
            </Border>
            <Expander x:Name="Expander1" Header="Others" Margin="0,0,10,0">
                <Button  Margin="0,0,0,0"  Width="{Binding ActualWidth, ElementName=RightStackPanel}" Background="White" Content="Add" Height="50" Click="Button_Click" ></Button>
            </Expander>

        </StackPanel>
    </ScrollViewer>

    <Frame  Grid.Column="0" x:Name="LeftFrame" Background="LightGray"  ></Frame>
    <Frame  Grid.Column="1" x:Name="CenterFrame" Background="DarkGray" ></Frame>

</Grid></Window>

Other Xaml file 其他Xaml文件

<Page
  x:Name="Page"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  mc:Ignorable="d"
  Title="Any" d:DesignWidth="1364" d:DesignHeight="868"
  >

<Grid>
    <Frame   Background="DarkGray" />

</Grid></Page>

MainWindow.xaml.cs MainWindow.xaml.cs

private void Button_Click(object sender, RoutedEventArgs e)
    {
        Frame middleFrame=CenterColumn;
        Otherxaml other=new Otherxaml();
        middleFrame.Source = new Uri("OtherxamlPage.xaml", UriKind.RelativeOrAbsolute);
    }

Pertinent to your code snippet, you may place the OtherxamlPage.xaml inside the central frame and set the properties of that frame like shown below: 与您的代码段相关,您可以将OtherxamlPage.xaml放在中心框架内并设置该框架的属性,如下所示:

<Frame  Grid.Column="1" x:Name="CenterFrame" VerticalAlignment="Stretch" VerticalContentAlignment="Center" HorizontalAlignment="Stretch"  HorizontalContentAlignment="Center" Source="OtherxamlPage.xaml" Background="DarkGray" />

You can set the Source=" OtherxamlPage.xaml " dynamically in event handler, eg Button.Click as per your example. 您可以在事件处理程序中动态设置Source =“ OtherxamlPage.xaml ”,例如Button.Click根据您的示例。

Alternatively, consider the creation of WPF UserControl (re: https://msdn.microsoft.com/en-us/library/cc294992.aspx ) instead of that other XAML Window (or Page) and place it directly into the grid cell. 或者,考虑创建WPF UserControl (re: https//msdn.microsoft.com/en-us/library/cc294992.aspx )而不是其他XAML窗口(或页面),并将其直接放入网格单元格中。 In both cases set the content "Stretch" property in order to adjust its size automatically, thus you won't need to specify it in the code. 在这两种情况下都设置内容“Stretch”属性以便自动调整其大小,因此您无需在代码中指定它。

Hope this may help. 希望这可能有所帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM