首先,这是我第一天使用 Xaml,所以这个问题对你来说可能是假的,但我完全迷路了。
概览
我的技术是我有 MainWindow.xaml,它分为三个区域(使用网格列),列宽自动设置。
根据右栏中的一些操作,中间栏显示一个页面,假设存在于不同命名空间中的 Page.xaml。
我在寻找什么
问题是我需要将此页面的宽度和高度设置为等于中间列的宽度和高度,因为它适合这个区域。
注意事项
我对 xaml 和绑定(bind)技术的经验很少。
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
(回复:https://msdn.microsoft.com/en-us/library/cc294992.aspx)而不是其他 XAML 窗口(或页面),并将其直接放入网格单元格中。在这两种情况下,都设置内容的“Stretch”属性以自动调整其大小,因此您无需在代码中指定它。
希望这可能有所帮助。
关于c# - 如何将 WPF 窗口嵌入另一个窗口并通过 XAML 或代码调整其大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36025653/