WPF控件操做之改變父控件之TabControl示例

昨天作個簡單的演示,想偷懶,但同時又想用WPF再作一個,稍微提升一下演示的美觀,助力拿下訂單。因此,在作完WinForm後,又作了一個wpf版本的,但控件在不一樣容器之間的移動,winform和wpf差異很大,winform很簡單、直觀。但wpf就很坑了。c#

因而昨天下午網上搜了一些「答案」,都不能解決問題。今天又從新理了一下頭緒,很是簡單的實現了,並無用到網上那些很裝逼的答案。spa

效果圖code

,要實現的效果就是經過TabControl的Select事件,讓最子層的界面在第二層TabControl的不一樣TAbItem之間移動,而第二層TabControl又在最頂層的TabControl的TabItem之間移動。僞造每一個頁面都有內容的假象,而且用黑體字符串來標識不一樣的TabItem。orm

XAML代碼blog

 1 <TabControl Grid.Row="1" Name="tabControl1" SelectedIndex="0" SelectionChanged="tabControl1_SelectionChanged">
 2             <TabItem Header="生產模式">
 3                 <TabControl x:Name="tabControl2" SelectedIndex="0" SelectionChanged="tabControl1_SelectionChanged">
 4                         <TabItem Header="單站查詢">
 5                             <DockPanel x:Name="container" LastChildFill="True">
 6                                 <Grid DockPanel.Dock="Top">
 7                                     <Grid.RowDefinitions>
 8                                         <RowDefinition/>
 9                                         <RowDefinition/>
10                                     </Grid.RowDefinitions>
11                                     <Grid.ColumnDefinitions>
12                                         <ColumnDefinition Width="2*"/>
13                                         <ColumnDefinition Width="2*"/>
14                                         <ColumnDefinition Width="4*"/>
15                                         <ColumnDefinition Width="1*"/>
16                                     </Grid.ColumnDefinitions>
17                                     <Label Grid.Row="0" Grid.Column="0" Content="不良品查詢方式"/>
18                                     <ComboBox Grid.Row="1" Grid.Column="0" SelectedIndex="0">
19                                         <ComboBoxItem Content="按固定時間查詢"/>
20                                         <ComboBoxItem Content="按批次查詢"/>
21                                         <ComboBoxItem Content="按產品型號查詢"/>
22                                     </ComboBox>
23                                     <Label Grid.Row="0" Grid.Column="1" Content="選擇日期"/>
24                                     <DatePicker Grid.Row="1" Grid.Column="1"/>
25                                     <Label Grid.Row="0" Grid.Column="2" Grid.RowSpan="2" x:Name="lblTitle" Background="AliceBlue" FontWeight="ExtraBold" FontSize="20" VerticalAlignment="Center" HorizontalAlignment="Center"/>
26                                     <Button Content="生成報表" Grid.Column="3" Grid.Row="1"/>
27                                 </Grid>
28                                 <DataGrid x:Name="grid"/>
29                             </DockPanel>
30                         </TabItem>
31                         <TabItem Header="全部數據查詢"></TabItem>
32                         <TabItem Header="不良數據查詢"></TabItem>
33                         <TabItem Header="巡檢件查詢"></TabItem>
34                         <TabItem Header="報廢記錄查詢"></TabItem>
35                         <TabItem Header="返工記錄查詢"></TabItem>
36                         <TabItem Header="MES數據查詢"></TabItem>
37                         <TabItem Header="產量查詢"></TabItem>
38                 </TabControl>
39             </TabItem>
40             <TabItem Header="補單模式">
41             </TabItem>
42         </TabControl>

c#代碼事件

 1         private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
 2         {
 3             TabControl tab = sender as TabControl;
 4             TabItem tabItem = tab.SelectedItem as TabItem;
 5             if (tabItem != null)
 6             {
 7                 if (tab == tabControl1)
 8                 {
 9                     (tab.SelectedItem as ContentControl).Content = tabControl2;
10                 }
11                 else
12                 {
13                     (tab.SelectedItem as ContentControl).Content = container;
14                 }
15             }
16 
17             if (tabControl1.SelectedIndex != -1 && tabControl2.SelectedIndex != -1)
18             {
19                 lblTitle.Content = (tabControl1.SelectedItem as TabItem).Header + "->" + (tabControl2.SelectedItem as TabItem).Header;
20             }
21         }
相關文章
相關標籤/搜索