2017-08-29 194 views
0

我有以下XAMLTabControl,它綁定到ObservableCollection並創建我的選項卡很好。如何通過XAML在WPF TabControl內添加控件

<Window x:Class="BA_Auditing.AuditWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="BizeAsset - Audit Results" Height="700" Width="1120" WindowStartupLocation="CenterScreen" WindowState="Maximized"> 
    <Grid> 
     <TabControl Name="ModuleTabControl" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5" ItemsSource="{Binding}" > 
      <TabControl.ItemTemplate> 
       <DataTemplate> 
        <TextBlock>        
         <TextBlock Text="{Binding DISPLAY_NAME}"/> 
        </TextBlock> 
       </DataTemplate> 
      </TabControl.ItemTemplate> 

      <TabControl.ContentTemplate> 
       <DataTemplate> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition /> 
          <RowDefinition Height="Auto" /> 
          <RowDefinition /> 
         </Grid.RowDefinitions> 
         <Grid Grid.Row="0"> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition /> 
           <ColumnDefinition /> 
          </Grid.ColumnDefinitions> 
          <TextBlock Grid.Column="0" Text="Search:" HorizontalAlignment="Right"/> 
          <TextBox x:Name="tbxSearch" Grid.Column="1"/> 
         </Grid> 
         <TextBlock Grid.Row="2" Text="Items Selected: 0 of 908" /> 
        </Grid> 
       </DataTemplate> 
      </TabControl.ContentTemplate> 
     </TabControl> 
    </Grid> 
</Window> 

接着我想填充每個標籤區域與控制下一級,其中將包括LabelTextBox另一TabControlTextBlock

我以前在WinForms寫這個,這是什麼樣子:

enter image description here

什麼XAML我是否需要添加到做到這一點?
那是因爲我是通過結合,而不是字面添加TabItem

[編輯]
我試圖進入控制到在的TabItem的身體TabControl.ContentTemplate但是沒有顯示動態設計它。
enter image description here

回答

2

我想如果你有"WW - Wastewater"選項卡上的「點擊」,你會看到正在生成的東西(的搜索框,等等) - 這是因爲在默認情況下未選中的選項卡。

無論如何,這裏有一些代碼讓你更接近你想要的 - 這只是爲了讓你開始,你需要添加其他管道代碼(更改通知等)。

我不知道你打算在「服務」選項卡中有什麼......所以不知道你是否可以用同樣的方式處理它們,例如「資產」。你也可能想明確定義網格列的名稱,而不是讓它們自動生成 - 在其他地方你可以找到一些技術來做到這一點。

enter image description here

<Window x:Class="WpfApp38.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:local="clr-namespace:WpfApp38" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <TabControl Name="ModuleTabControl" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5" ItemsSource="{Binding}" SelectedIndex="0" > 
      <TabControl.ItemTemplate> 
       <DataTemplate> 
        <TextBlock>        
         <TextBlock Text="{Binding DISPLAY_NAME}"/> 
        </TextBlock> 
       </DataTemplate> 
      </TabControl.ItemTemplate> 

      <TabControl.ContentTemplate> 
       <DataTemplate> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="Auto"/> 
          <RowDefinition Height="*" /> 
          <RowDefinition Height="Auto"/> 
         </Grid.RowDefinitions> 
          <Grid Grid.Row="0"> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition /> 
            <ColumnDefinition /> 
           </Grid.ColumnDefinitions> 
           <TextBlock Grid.Column="0" Text="Search:" HorizontalAlignment="Right"/> 
           <TextBox x:Name="tbxSearch" Grid.Column="1"/> 
          </Grid> 
         <TabControl Grid.Row="1" ItemsSource="{Binding SubCategories}"> 
          <TabControl.ItemTemplate> 
           <DataTemplate> 
            <TextBlock Text="{Binding DISPLAY_NAME}"/> 
           </DataTemplate> 
          </TabControl.ItemTemplate> 
          <TabControl.ContentTemplate> 
           <ItemContainerTemplate> 
            <DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Assets}"> 
            </DataGrid> 
           </ItemContainerTemplate> 
          </TabControl.ContentTemplate> 
         </TabControl> 
         <TextBlock Grid.Row="2" Text="Items Selected: 0 of 908" /> 
        </Grid> 
       </DataTemplate> 
      </TabControl.ContentTemplate> 
     </TabControl> 
    </Grid> 
</Window> 

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace WpfApp38 
{ 
    public class InfrastructureCateogry 
    { 
     public string DISPLAY_NAME { get; set; } 

     public ObservableCollection<AssetCategory> SubCategories { get; set; } 
    } 

    public class AssetCategory 
    { 
     public string DISPLAY_NAME { get; set; } 

     public ObservableCollection<AssetRecord> Assets { get; set; } 
    } 

    public class AssetRecord 
    { 
     public string AssetID { get; set; } // make it an int 
     public string AssetType { get; set; } 
     public string LastUpdateBy { get; set; } // make this a DateTime object 
     public string LastUpdateDate { get; set; } // make this a DateTime object 
    } 

    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     private ObservableCollection<InfrastructureCateogry> infrastructurecategories = new ObservableCollection<InfrastructureCateogry>(); 

     public MainWindow() 
     { 
      InitializeComponent(); 

      var x = new InfrastructureCateogry() 
      { 
       DISPLAY_NAME = "AR - Roads and Bridges", 
       SubCategories = new ObservableCollection<AssetCategory> 
       { 
        new AssetCategory 
        { 
         DISPLAY_NAME = "Lines", 
         Assets = new ObservableCollection<AssetRecord> 
         { 
          new AssetRecord 
          { 
           AssetID = "20040927104600", 
           AssetType = "Gravity Main", 
           LastUpdateDate = "07/05/2015 17:01:55 PM" 
          }, 
          new AssetRecord 
          { 
           AssetID = "20150507170116", 
           AssetType = "Relined", 
           LastUpdateDate = "07/05/2015 17:01:15 PM" 
          } 
         } 
        }, 
        new AssetCategory 
        { 
         DISPLAY_NAME = "Points" 
        }, 
        new AssetCategory 
        { 
         DISPLAY_NAME = "Plant/Components" 
        }, 
        new AssetCategory 
        { 
         DISPLAY_NAME = "Services" 
        } 
       } 
      }; 

      infrastructurecategories.Add(x); 

      var x2 = new InfrastructureCateogry(); 
      x2.DISPLAY_NAME = "WW - WasteWater"; 

      infrastructurecategories.Add(x2); 

      this.DataContext = infrastructurecategories; 
     } 
    } 
} 
+0

嗯,我覺得有點傻......我爲什麼不只是一下就可以了哈!不要以爲你知道在XAML中默認選擇第一個選項卡的方法。有一點奇怪,它不能正確顯示窗口中的第一個交互控件。 – Hank

+0

TabControl上的SelectedIndex =「0」 –

+0

非常感謝! – Hank

相關問題