在 Dockpanel 中拉伸用户控件

Stretch Usercontrol in Dockpanel

我是 WPF 的新手。我从 Forms 知道我可以将用户控件添加到面板。我如何在 WPF 中执行此操作?我尝试了 Grid、DockPanel 和 StackPanel,但我不知道如何扩展我的用户控件?在这个 Grid order 中,还有什么只会是这个 usercontrol。

我需要切换网格的内容,否则因为我想显示不同的用户控件。

主要XAML

<Window x:Class="TaxHelper.MainWindow"
        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"
        xmlns:entites="clr-namespace:TaxHelper.Entity"
        xmlns:local="clr-namespace:TaxHelper"
        xmlns:properties="clr-namespace:TaxHelper.Properties"
        mc:Ignorable="d"
        Title="TaxHelper" Height="558" Width="803">
    <Window.Resources>
        <local:InvoiceStatusIconConverter x:Key="IconConverter"/>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="251*"/>
            <ColumnDefinition Width="292*"/>
        </Grid.ColumnDefinitions>
        <DockPanel LastChildFill="True">
            <Menu DockPanel.Dock="Top" Height="20" Margin="0,0,10,0">
                <MenuItem Header="Datebank" Height="20">
                    <MenuItem Header="Importieren" Name="miImport" Click="miImport_Click"/>
                </MenuItem>

                <MenuItem Header="Daten" Height="20">
                    <MenuItem Header="Laden" Name="miLoadData" Click="miLoadData_Click"/>
                </MenuItem>

                <MenuItem Header="Tests" Height="20">
                    <MenuItem Header="Add Usercontrol" Name="miTestbutton" Click="miTestbutton_Click"/>
                </MenuItem>
            </Menu>
            <StackPanel></StackPanel>
        </DockPanel>
        <TreeView x:Name="tvNavigation" Margin="10,26,10,10" HorizontalContentAlignment="Stretch" TreeViewItem.Expanded="tvNavigation_Expanded">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type entites:Owner}" ItemsSource="{Binding Items}">
                    <TextBlock Text="{Binding Name}"/>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type entites:EconomyUnit}" ItemsSource="{Binding Items}">
                    <TextBlock Text="{Binding Name}"/>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type entites:Report}" ItemsSource="{Binding Items}">
                    <TextBlock Text="{Binding Name}"/>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type entites:ReportItem}" ItemsSource="{Binding Items}">
                    <TextBlock Text="{Binding Name}"/>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type entites:Invoice}" ItemsSource="{Binding Items}">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="{Binding Status, Converter={StaticResource IconConverter}}" MaxHeight="16px" MaxWidth="16px" HorizontalAlignment="Left"></Image>
                        <TextBlock Text="{Binding Company}"/>
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.Resources>
        </TreeView>
        <!--<StackPanel x:Name="dpContent" Orientation="Vertical" HorizontalAlignment="Stretch" Height="507" Margin="10,10,10,0" VerticalAlignment="Top" Width="408" Grid.Column="1"/>-->
        <Grid Height="Auto" Name="dpContent" Width="Auto" Grid.Column="1" Margin="24,26,29,10">

        </Grid>
    </Grid>
</Window>

用户控件:

<UserControl x:Class="TaxHelper.Invoice"
        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" 
        xmlns:local="clr-namespace:TaxHelper"
        xmlns:mpp="clr-namespace:MoonPdfLib;assembly=MoonPdfLib"
        mc:Ignorable="d" Height="152.438" Width="132.531">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="81*"/>
            <RowDefinition Height="14*"/>
        </Grid.RowDefinitions>

        <mpp:MoonPdfPanel Background="LightGray" ViewType="SinglePage" PageRowDisplay="ContinuousPageRows" PageMargin="0,2,4,2" AllowDrop="True" x:Name="mpp" x:FieldModifier="private"/>
        <StackPanel Margin="0,1,0,0" Grid.Row="1">
            <Button  Content="Exit" Click="Button_Click" Height="12" FontSize="5" />
        </StackPanel>
    </Grid>
</UserControl>

添加用户控件:

private void miTestbutton_Click(object sender, RoutedEventArgs e)
{
    Invoice invoice = new Invoice();
    invoice.HorizontalAlignment = HorizontalAlignment.Stretch;
    invoice.VerticalAlignment = VerticalAlignment.Stretch;

    dpContent.Children.Add(invoice);
}

UserContol 的声明正在为宽度和高度设置硬编码值,这意味着控件无法自行调整大小。

如果将这两个属性替换为 d:DesignWidthd:DesignHeight,那么这些值将变为 "design-time only" 值,因此当您在 [=19] 中查看控件时将使用这些值=] 设计师,但不是 run-time...

的应用程序
<UserControl x:Class="TaxHelper.Invoice"
     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" 
     xmlns:local="clr-namespace:TaxHelper"
     xmlns:mpp="clr-namespace:MoonPdfLib;assembly=MoonPdfLib"
     mc:Ignorable="d" d:DesignHeight="152.438" d:DesignWidth="132.531">