2016-01-24 51 views
0

我是WPF中的新成員,我無法工作這個希望你們可以幫助解決這個問題。問題在於DesignerItemTemplate在項目控件中不起作用,我試圖直接使用這個項目來拖動它。如果代碼看起來凌亂,請原諒我。在此先感謝WPF Itemcontrol datatemplate

<UserControl.Resources> 
    <ControlTemplate x:Key="MoveThumbTemplate" TargetType="{x:Type s:MoveThumb}"> 
     <Rectangle Fill="Transparent" /> 
    </ControlTemplate> 

    <!-- ResizeDecorator Template --> 
    <ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="{x:Type Control}"> 
     <Grid> 
      <StackPanel VerticalAlignment="Top" HorizontalAlignment="Left" Height="15" Width="130" Margin="0,-15,-81,0" IsHitTestVisible="False"> 
       <TextBox Text="R101" BorderBrush="Transparent" IsHitTestVisible="False" Background="Transparent" Height="17" FontSize="7" Foreground="#FF6DF90C" VerticalContentAlignment="Stretch" MinHeight="1" HorizontalAlignment="Stretch" CharacterCasing="Upper" /> 
      </StackPanel> 

      <s:ResizeThumb Height="1" Cursor="SizeNS" BorderBrush="#FF160DCD" BorderThickness="1" 
          VerticalAlignment="Top" HorizontalAlignment="Stretch"/> 
      <s:ResizeThumb Width="1" Cursor="SizeWE" BorderBrush="#FF160DCD" BorderThickness="1" 
          VerticalAlignment="Stretch" HorizontalAlignment="Left"/> 
      <s:ResizeThumb Width="1" Cursor="SizeWE" BorderBrush="#FF160DCD" BorderThickness="1" 
          VerticalAlignment="Stretch" HorizontalAlignment="Right"/> 
      <s:ResizeThumb Height="1" Cursor="SizeNS" BorderBrush="#FF160DCD" BorderThickness="1" 
          VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/> 
      <s:ResizeThumb Width="2" Height="2" Cursor="SizeNWSE" BorderBrush="#FF81F110" BorderThickness="1" 
          VerticalAlignment="Top" HorizontalAlignment="Left"/> 
      <s:ResizeThumb Width="2" Height="2" Cursor="SizeNESW" BorderBrush="#FF81F110" BorderThickness="1" 
          VerticalAlignment="Top" HorizontalAlignment="Right"/> 
      <s:ResizeThumb Width="2" Height="2" Cursor="SizeNESW" BorderBrush="#FF81F110" BorderThickness="1" 
          VerticalAlignment="Bottom" HorizontalAlignment="Left"/> 
      <s:ResizeThumb Width="2" Height="2" Cursor="SizeNWSE" BorderBrush="#FF81F110" BorderThickness="1" 
          VerticalAlignment="Bottom" HorizontalAlignment="Right"/> 
     </Grid> 
    </ControlTemplate> 

    <!-- Designer Item Template--> 
    <ControlTemplate x:Key="DesignerItemTemplate" TargetType="{x:Type ContentControl}"> 
     <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"> 
      <s:MoveThumb Template="{StaticResource MoveThumbTemplate}" Cursor="SizeAll"/> 
      <Control Template="{StaticResource ResizeDecoratorTemplate}"/> 
      <ContentPresenter Content="{TemplateBinding Content}"/> 
     </Grid> 
    </ControlTemplate> 
</UserControl.Resources> 

<panZoom:PanAndZoomViewer x:Name="panZoomViewer" Margin="2,2,2,45" ClipToBounds="True"> 
    <!--<Canvas>--> 
    <Canvas x:Name="cnvImage" Background="Transparent"> 
     <Image x:Name="imgCurrent" VerticalAlignment="Center" HorizontalAlignment="Center" /> 
     <ItemsControl ItemsSource="{Binding InspectionItemList,Mode=TwoWay }"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <ContentControl Canvas.Top="{Binding Y,Mode=TwoWay}" Canvas.Left="{Binding X,Mode=TwoWay }" 
            Width="{Binding Width,Mode=TwoWay}" MinWidth="1" 
            Height="{Binding Height,Mode=TwoWay}" MinHeight="1" 
            Template="{Binding Mode=OneWay, Source={StaticResource DesignerItemTemplate}}" /> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </Canvas> 
    <!--</Canvas>--> 
</panZoom:PanAndZoomViewer> 
+0

它應該看起來像這樣:http://stackoverflow.com/a/34755874/1136211 – Clemens

+0

嗨克萊門斯,我跟着結構和所有的datatemplate現在不會顯示。 –

回答

0

代替

Template="{Binding Mode=OneWay, Source={StaticResource DesignerItemTemplate}}" />
試試這個:

Template="{StaticResource DesignerItemTemplate}" />

如果這沒有幫助,你可以請提供證明的問題稍微簡單例子,說明你想才達到的。我不清楚你想要達到什麼目的。

0

我發現問題了!它是繼承到可觀察對象的InspectionItemList集合對象。當我刪除對象中的obeservable時,它完美地工作。感謝克萊門斯和Stackoverflow這個網站真的幫了我很多。