繁体   English   中英

添加contentcontrol后,xaml controltemplate元素消失

[英]xaml controltemplate element disappear after adding contentcontrol

我将以下控制模板应用于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>

但是,一旦添加内容控件,图像和文本块元素就会消失。 调整器的装饰效果很好。

内容控件似乎位于内容控件内部的堆栈面板顶部,或者根本不显示它。

我不知道为什么会发生这种情况,任何人都可以向正确的方向指点吗?

这是因为Template会覆盖ContentControl的全部内容。 您必须使用ContentPresenter将内容放置在ControlTemplate中,如下所示:

<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>

还要注意, ControlTemplateTargetType应该是ContentControl (而不仅仅是Control )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM