2012-09-24 36 views
0

我有一個我不明白的WPF問題 - 任何人都可以幫忙嗎?TabControl DataTemplate實例創建問題

下面的WPF用作標準TabControl的ContentTemplate並駐留在ResourceDictionary中。 MyElementItemsControl是ItemsControl的簡單派生,MyDesignCanvas是Canvas的簡單派生。

<DataTemplate x:Key="TabContent" x:Shared="False"> 
    <Grid> 
     <Grid Grid.RowSpan="2"> 
      <ScrollViewer x:Name="contentScrollViewer" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" > 
       <Grid> 
        <View:MyElementItemsControl BorderBrush="Transparent" x:Name="schedulePanel" ItemsSource="{Binding Path=Elements}" Background="White"> 
         <View:MyElementItemsControl.ItemsPanel> 
          <ItemsPanelTemplate> 
           <View:MyDesignCanvas Height="1000" Width="1000" HorizontalAlignment="Left" VerticalAlignment="Top" 
                    SelectedItem="{Binding SelectedItem, Mode=TwoWay}" 
                    Background="Transparent"> 
           </View:MyDesignCanvas> 
          </ItemsPanelTemplate> 
         </View:MyElementItemsControl.ItemsPanel> 
        </View:MyElementItemsControl> 

        <Grid.LayoutTransform> 
         <TransformGroup> 
          <ScaleTransform> 
           <ScaleTransform.ScaleX> 
            <Binding ElementName="SlideZoom" Path="Value" Mode="OneWay"/> 
           </ScaleTransform.ScaleX> 
           <ScaleTransform.ScaleY> 
            <Binding ElementName="SlideZoom" Path="Value" Mode="OneWay"/> 
           </ScaleTransform.ScaleY> 
          </ScaleTransform> 
         </TransformGroup> 
        </Grid.LayoutTransform> 

       </Grid> 
      </ScrollViewer> 

     </Grid> 

     <Slider Opacity="0.5" VerticalAlignment="Top" HorizontalAlignment="Left" Width="300" Grid.Row="1" Name="SlideZoom" Orientation="Horizontal" Minimum="0.1" Maximum="3" Value="1"> 
     </Slider> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="30" /> 
     </Grid.RowDefinitions> 

    </Grid> 
</DataTemplate> 

當我運行代碼,我得到兩個問題我不明白:

  1. 我似乎只得到一個ScrollViewer中時,我希望每一個項目的ScrollViewer。因此,如果我添加兩個選項卡項並使畫布的大小不同,則滾動條僅調整爲最大畫布的大小。我預計Shared = False屬性會爲每個選項卡創建模板的新實例。
  2. 也許與第1項有關 - 如果我在MyDesignCanvas的構造函數上插入一個斷點,它會在添加第一個選項卡時被觸發,但在添加其他選項卡時不會被觸發。只有當我開始關閉選項卡時,斷點纔會再次出現 - 我期望在每個選項卡上添加命中。

我想我不太瞭解足夠的數據模板,所以任何人都可以解釋可能會發生什麼,或者指向某些資源,可以幫助我診斷此問題?

感謝

回答

3

我已經意識到了什麼問題是 - WPF的TabControl做的標籤內容的內部虛擬化,所以又被重新使用的標籤內容,只是改變使用共享=假,儘管我的數據上下文。有關更多詳細信息,請參閱this SO questionthis one too