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

首先,这是我使用Xaml的第一天所以这个问题对你来说可能是假的,但我完全迷路了.

概观

我的技术是我有MainWindow.xaml,它被分成三个区域(使用网格列)自动设置列宽.

基于右列中的某些操作,显示页面的中间列让我们说不同命名空间中存在的Page.xaml.

我正在寻找什么

问题是我需要将此页面的宽度和高度设置为等于中间列宽度和高度,因为它将适合此区域.

笔记

我对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

<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

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

最佳答案 与您的代码段相关,您可以将OtherxamlPage.xaml放在中心框架内并设置该框架的属性,如下所示:

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

您可以在事件处理程序中动态设置Source =“OtherxamlPage.xaml”,例如Button.Click根据您的示例.

或者,考虑创建WPF UserControl(re:https://msdn.microsoft.com/en-us/library/cc294992.aspx)而不是其他XAML窗口(或页面),并将其直接放入网格单元格中.在这两种情况下都设置内容“Stretch”属性以便自动调整其大小,因此您无需在代码中指定它.

希望这可能有所帮助.

点赞