如何将 WPF Window 嵌入另一个 Window 并通过 XAML 或代码调整其大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36025653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to embed WPF Window into another Window and adjust its size via XAML or code
提问by Xexolas
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
主窗口.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
主窗口.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);
}
采纳答案by Alexander Bell
Pertinent to your code snippet, you may place the OtherxamlPage.xamlinside 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, e.g. Button.Clickas per your example.
您可以OtherxamlPage.xaml在事件处理程序中动态设置 Source=" ",例如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. 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.
或者,考虑创建 WPF UserControl(re:https: //msdn.microsoft.com/en-us/library/cc294992.aspx)而不是其他 XAML 窗口(或页面)并将其直接放入网格单元中。在这两种情况下,都设置内容“Stretch”属性以自动调整其大小,因此您无需在代码中指定它。
Hope this may help.
希望这可能会有所帮助。

