首先,这是我第一天使用xaml,所以这个问题对你来说可能是个哑巴,但我完全迷路了。
概述
我的技巧是我有mainwindow.xaml,它被分成三个区域(使用网格列),列宽度是自动设置的。
基于右边列中的一些操作,中间列显示一个页面,让我们假设Page.xaml存在于不同的命名空间中。
我在寻找什么
问题是我需要将此页的宽度和高度设置为与中间列的宽度和高度相等,因为它将适合此区域。
笔记
我对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>

其他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>

主窗口.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
或者,考虑创建wpfUserControl(re:https://msdn.microsoft.com/en-us/library/cc294992.aspx)而不是其他的xaml窗口(或页面),并将其直接放入网格单元中。在这两种情况下,都可以设置content“stretch”属性以自动调整其大小,因此不需要在代码中指定它。
希望这能有所帮助。

关于c# - 如何将WPF窗口嵌入另一个窗口并通过XAML或代码调整其大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36025653/

10-14 16:12
查看更多