C# 以所需顺序在WPF中显示数据

C# 以所需顺序在WPF中显示数据,c#,wpf,xaml,C#,Wpf,Xaml,在我的C#/WPF应用程序中有以下xaml代码 我需要能够在这些文本块和文本框之后显示View1的内容。取而代之的是,View1的内容现在显示在StackPanel内容之后。 请问我这里缺什么? 谢谢你的帮助 请问我这里缺什么 不要将ContentPresenter放在网格中 事实是,没有一个好的答案是不可能确定的。但是根据您发布的内容,看起来您只是将控件放在了XAML中的错误位置 即,布局应如下所示: <Grid Margin="5"> <Grid.RowDef

在我的C#/WPF应用程序中有以下xaml代码

我需要能够在这些文本块和文本框之后显示View1的内容。取而代之的是,View1的内容现在显示在StackPanel内容之后。 请问我这里缺什么? 谢谢你的帮助



请问我这里缺什么

不要将
ContentPresenter
放在
网格中

事实是,没有一个好的答案是不可能确定的。但是根据您发布的内容,看起来您只是将控件放在了XAML中的错误位置

即,布局应如下所示:

<Grid Margin="5">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>

    <StackPanel Grid.Row="0"  HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Horizontal" Height="28" Width="auto" Margin="5,0,0,0">
        <!-- contents of StackPanel -->
    </StackPanel>

    <Grid Margin="5" Grid.Row="1" >
        <!-- contents of inner Grid -->
    </Grid>
    <ContentPresenter Content="{Binding CurrentViewModel}" Grid.Row="2">
        <ContentPresenter.Resources>
            <DataTemplate DataType="{x:Type ViewModel:VM1}">
                <Views:View1/>
            </DataTemplate>
            <DataTemplate DataType="{x:Type ViewModel:VM2">
                <Views:View2/>
            </DataTemplate>                  
        </ContentPresenter.Resources>
    </ContentPresenter>
</Grid>


感谢您的输入,Peter。抱歉,我也尝试了同样的方法,但没有成功。您的布局确实需要一些清理,以便合理使用。网格中的列和行太多了。您应该将控件分组到有组织的子容器中,如嵌套的StackPanel等。这将是一个需要维护的噩梦。对于初学者来说,这个庞大的控件列表甚至不在定义了所有列和行的网格中。将其全部放在没有列和行的嵌套网格中。
<Grid Margin="5">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>

    <StackPanel Grid.Row="0"  HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Horizontal" Height="28" Width="auto" Margin="5,0,0,0">
        <!-- contents of StackPanel -->
    </StackPanel>

    <Grid Margin="5" Grid.Row="1" >
        <!-- contents of inner Grid -->
    </Grid>
    <ContentPresenter Content="{Binding CurrentViewModel}" Grid.Row="2">
        <ContentPresenter.Resources>
            <DataTemplate DataType="{x:Type ViewModel:VM1}">
                <Views:View1/>
            </DataTemplate>
            <DataTemplate DataType="{x:Type ViewModel:VM2">
                <Views:View2/>
            </DataTemplate>                  
        </ContentPresenter.Resources>
    </ContentPresenter>
</Grid>