我有一个我在Thumb类上应用的控件模板:
<ControlTemplate x:Key="StateShape">
<StackPanel>
<Image Name="tplImage" Source="/Images/state.png" Stretch="Uniform" Width="128" Height="128" HorizontalAlignment="Center"/>
<TextBlock Name="tplTextBlock" Text="User stage" HorizontalAlignment="Center"/>
</StackPanel>
</ControlTemplate>
我正在尝试使用以下内容为此缩放器添加标记:
<ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="{x:Type Control}">
<Grid>
<c:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 -4 0 0"
VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
<c:ResizeThumb Width="3" Cursor="SizeWE" Margin="-4 0 0 0"
VerticalAlignment="Stretch" HorizontalAlignment="Left"/>
<c:ResizeThumb Width="3" Cursor="SizeWE" Margin="0 0 -4 0"
VerticalAlignment="Stretch" HorizontalAlignment="Right"/>
<c:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 0 0 -4"
VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/>
<c:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="-6 -6 0 0"
VerticalAlignment="Top" HorizontalAlignment="Left"/>
<c:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="0 -6 -6 0"
VerticalAlignment="Top" HorizontalAlignment="Right"/>
<c:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="-6 0 0 -6"
VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
<c:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="0 0 -6 -6"
VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
</Grid>
</ControlTemplate>
我尝试以下列方式申请此模板:
<ControlTemplate x:Key="StateShape">
<ContentControl Width="128"
Height="128"
Template="{StaticResource ResizeDecoratorTemplate}">
<StackPanel>
<Image Name="tplImage" Source="/Images/state.png" Stretch="Uniform" Width="128" Height="128" HorizontalAlignment="Center"/>
<TextBlock Name="tplTextBlock" Text="User stage" HorizontalAlignment="Center"/>
</StackPanel>
</ContentControl>
</ControlTemplate>
然而,只要我添加内容控件,图像和textblock元素就会消失。 调整器的装饰很好地显示出来。
似乎contentcontrol位于contentcontrol内部的stackpanel之上,或者根本不显示它。
我不知道为什么会发生这种情况,任何人都可以指向正确的方向吗?
答案 0 :(得分:2)
这是因为Template
会覆盖ContentControl
的全部内容。您必须使用ControlTemplate
将内容放在ContentPresenter
内,如下所示:
<ControlTemplate x:Key="ResizeDecoratorTemplate"
TargetType="{x:Type ContentControl}">
<Grid>
<c:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 -4 0 0"
VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
<c:ResizeThumb Width="3" Cursor="SizeWE" Margin="-4 0 0 0"
VerticalAlignment="Stretch" HorizontalAlignment="Left"/>
<c:ResizeThumb Width="3" Cursor="SizeWE" Margin="0 0 -4 0"
VerticalAlignment="Stretch" HorizontalAlignment="Right"/>
<c:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 0 0 -4"
VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/>
<c:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="-6 -6 0 0"
VerticalAlignment="Top" HorizontalAlignment="Left"/>
<c:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="0 -6 -6 0"
VerticalAlignment="Top" HorizontalAlignment="Right"/>
<c:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="-6 0 0 -6"
VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
<c:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="0 0 -6 -6"
VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
<!-- we place the ContentPresenter here -->
<ContentPresenter/>
</Grid>
</ControlTemplate>
另请注意,TargetType
的{{1}}应为ControlTemplate
(不仅仅是ContentControl
)。