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

时间:2016-08-08 21:31:30

标签: c# wpf xaml

我在C#/ WPF应用程序中使用了以下xaml代码。

我需要能够在那些文本块和文本框之后显示View1的内容。相反,View1的内容现在出现在StackPanel内容之后。 我在这里想念的是什么? 谢谢你的帮助。

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:MainViewModel="clr-namespace:MyTool.ViewModel"
        xmlns:ViewModel="clr-namespace:MyTool.ViewModel.MyProducts"
        xmlns:View="clr-namespace:MyTool.View" x:Class="MyTool.MainWindow"
        xmlns:Views="clr-namespace:MyTool.View.MyProducts"
        xmlns:v="clr-namespace:MyTool.View.MyProducts"
        xmlns:vm="clr-namespace:MyTool.ViewModel.MyProducts"
        Title="{Binding DisplayName, Mode=OneWay}" ResizeMode="CanResize" WindowStartupLocation="CenterScreen">
    <Window.DataContext>
        <MainViewModel:MyViewModel/>
    </Window.DataContext>
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <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">

        <TextBlock TextWrapping="Wrap" Text="Security Type" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,0,5,0" Style="{DynamicResource DefaultTextBlockStyle}"/>
            <ComboBox Width="360" Margin="1,0" ItemsSource="{Binding MyProductTypes}" DisplayMemberPath="Code" SelectedValuePath="ID" SelectedItem="{Binding SelectedProductType, Mode=TwoWay}" TabIndex="0" />





            <Button x:Name="btnSave" Content=" Save " Margin="25,0,5,0" Command="{Binding SaveCommand, Mode=OneWay}" TabIndex="31" />

            <Button Content=" Clear " Command="{Binding ClearCommand, Mode=OneWay}" Margin="0,0,5,0" TabIndex="34"/>
        </StackPanel>

        <Grid Margin="5" Grid.Row="1" >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="10"/>
                <RowDefinition Height="Auto" MinHeight="24"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="200"/>
                <ColumnDefinition Width="23"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="14*"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="71*"/>
            </Grid.ColumnDefinitions>
       <Grid Margin="5" Grid.Row="1" >

            <TextBlock  Grid.Column="6" HorizontalAlignment="Right" Grid.Row="0" TextWrapping="Wrap" VerticalAlignment="Bottom" Style="{DynamicResource SysdateMessageStyle}" Text="{Binding SystemDateMessage, Mode=OneWay}" Height="16" Width="0" />
            <TextBlock  Grid.Column="1" HorizontalAlignment="Left" TextWrapping="Wrap" Text="T1" VerticalAlignment="Top" Grid.Row="1" Style="{DynamicResource HeadingStyle}" Grid.RowSpan="1" Margin="1,10,0,0" Height="23" Width="112"/>
            <TextBlock Grid.Column="1" HorizontalAlignment="Left" Margin="1,1,0,0" Grid.Row="3" TextWrapping="Wrap" VerticalAlignment="Top" Text="T2" Style="{DynamicResource DefaultTextBlockStyle}" Height="18" Width="61"/>
            <TextBlock Grid.Column="1" HorizontalAlignment="Left" Margin="1,1,0,0" Grid.Row="4" TextWrapping="Wrap" VerticalAlignment="Top" Text="T3" Style="{DynamicResource DefaultTextBlockStyle}" Height="18" Width="89"/>

            <TextBlock Grid.Column="1" HorizontalAlignment="Left" Margin="1,1,0,0" Grid.Row="5" TextWrapping="Wrap" VerticalAlignment="Top" Text="T4" Style="{DynamicResource DefaultTextBlockStyle}" Height="18" Width="103"/>
            <TextBlock Grid.Column="1" HorizontalAlignment="Left" Margin="1,1,0,0" Grid.Row="6" TextWrapping="Wrap" VerticalAlignment="Top" Text="T5" Style="{DynamicResource DefaultTextBlockStyle}" Height="18" Width="197"/>



            <TextBlock Grid.Column="1" HorizontalAlignment="Left" Margin="1,1,0,0" Grid.Row="7" TextWrapping="Wrap" VerticalAlignment="Top" Text="T6" Style="{DynamicResource DefaultTextBlockStyle}" Height="18" Width="103"/>

            <TextBox Name="txtTargetTicker1" Grid.Column="3" IsEnabled="{Binding EnableDealView}" HorizontalAlignment="Left" Height="23" Margin="1,1,0,0" Grid.Row="3" TextWrapping="Wrap" VerticalAlignment="Top" Width="155" Text="{Binding F1}" Style="{DynamicResource TargetTickerStyle}" TabIndex="2"/>

            <TextBox Name="txtTargetTicker" Grid.Column="3" IsEnabled="{Binding F2}" HorizontalAlignment="Left" Height="23" Margin="1,1,0,0" Grid.Row="4" TextWrapping="Wrap" VerticalAlignment="Top" Width="155" Text="{Binding F3}" Style="{DynamicResource TargetTickerStyle}" TabIndex="2"/>
            <TextBox Grid.Column="3" IsEnabled="{Binding F4}" HorizontalAlignment="Left" Height="23" Margin="1,21,0,0" Grid.Row="4" TextWrapping="Wrap" VerticalAlignment="Top" Width="155" Text="{Binding F5}" Style="{DynamicResource AcquirerTickerStyle}" TabIndex="3" Grid.RowSpan="2"/>

            <TextBox Grid.Column="3" IsEnabled="{Binding F6}" HorizontalAlignment="Left" Margin="1,1,0,0" Grid.Row="6" Width="155" Text="{Binding F7}" TabIndex="4"/>

            <StackPanel Grid.Column="3" IsEnabled="{Binding F8}" Grid.Row="7" HorizontalAlignment="Left" VerticalAlignment="Center" FlowDirection="LeftToRight" Orientation="Horizontal" Margin="0,3,0,0" Width="250" Height="22">
                <TextBox Grid.Column="2" IsEnabled="{Binding F9}" HorizontalAlignment="Left" Height="23" Margin="1,1,0,0" Grid.Row="5" TextWrapping="Wrap" VerticalAlignment="Top" Width="155" Style="{DynamicResource AcquirerTickerStyle}" TabIndex="3"/>
            </StackPanel>

            <TextBlock Grid.Column="4" HorizontalAlignment="Left" Margin="1,1,0,0" Grid.Row="3" TextWrapping="Wrap" VerticalAlignment="Top" Style="{DynamicResource DefaultTextBlockStyle}" Height="18" Width="75" Grid.ColumnSpan="2">
            <Run Text="Asset Currency"/>
            <Run Text="{Binding F10}"/>
            </TextBlock>
            <TextBlock Grid.Column="4" HorizontalAlignment="Left" Margin="1,1,0,0" Grid.Row="4" TextWrapping="Wrap" VerticalAlignment="Top" Style="{DynamicResource DefaultTextBlockStyle}" Height="18" Width="87" Grid.ColumnSpan="2">
            <Run Text="Manager"/>
            <Run Text="{Binding F11}"/>
            </TextBlock>
            <TextBlock Grid.Column="4" HorizontalAlignment="Left" Margin="1,1,0,5" Grid.Row="5" TextWrapping="Wrap" Style="{DynamicResource DefaultTextBlockStyle}" Width="53" Grid.ColumnSpan="2">
            <Run Text="Bid Value"/>
            <Run Text="{Binding F12}"/>
            </TextBlock>

            <TextBlock Grid.Column="4" HorizontalAlignment="Left" Margin="1,1,0,0" Grid.Row="6" TextWrapping="Wrap" VerticalAlignment="Top" Text="Country of risk" Style="{DynamicResource DefaultTextBlockStyle}" Height="18" Width="120" Grid.ColumnSpan="2"/>


        </Grid>
            <ContentPresenter Content="{Binding CurrentViewModel}">
                <ContentPresenter.Resources>
                    <DataTemplate DataType="{x:Type ViewModel:VM1}">
                        <Views:View1/>
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type ViewModel:VM2}">
                        <Views:View2/>
                    </DataTemplate>                  
                </ContentPresenter.Resources>
            </ContentPresenter>

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

1 个答案:

答案 0 :(得分:1)

  

我在这里想念的是什么?

请勿将ContentPresenter放入Grid

事实是,没有好的Minimal, Complete and Verifiable code example,我们无法肯定地说出来。但根据你发布的内容,看起来你只是将控件放在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>