2016-08-08 42 views
1

我的C#/ WPF應用程序中有以下xaml代碼。以所需順序在WPF中顯示數據

我需要能夠在那些文本塊和文本框之後顯示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

你的佈局絕對需要一些清理,以明智地處理。你的網格中有太多的列和行。您應該將控件分組到組織的子容器中,如嵌套的StackPanels等。這將是一個噩夢來維持。 –

+0

對於初學者來說,那些大的控件列表甚至沒有定義好所有列和行的網格。你把它全部放在沒有列和行的嵌套網格中。 –

回答

1

缺少什麼我在這裏嗎?

不要把ContentPresenter放在Grid裏面嗎?

事實是,沒有一個好的Minimal, Complete and Verifiable code example這是不可能肯定地說。但是根據你發佈的內容,它看起來像你只是把控制放在XAML的錯誤位置。

I.e.佈局應該是這樣的:

<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> 
+0

感謝您的輸入Peter.Sorry,我嘗試過但沒有運氣。 –

+0

當然,我現在已經複製了我的完整xaml代碼。 –

相關問題