首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ContentControl包装问题

ContentControl包装问题
EN

Stack Overflow用户
提问于 2014-04-17 12:48:26
回答 2查看 2.4K关注 0票数 4

我正在尝试创建一个自定义工具提示控件。此控件是从ToolTip类继承的。我的自定义工具提示将有一个标题和一个内容区域。内容可以是普通文本或任何其他内容(图像、富文本框等)。下面是自定义工具提示控件的模板样式。

代码语言:javascript
复制
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type customControls:FlyoutHelp}">
                <Border BorderThickness="0" Margin="15" Width="300">
                    <Border.Effect>
                        <DropShadowEffect Opacity="0.7"  />
                    </Border.Effect>
                    <StackPanel  TextBlock.FontFamily="Trebuchet MS" TextBlock.FontSize='12'>
                        <TextBlock Background="{StaticResource DellBlue}" Height="23" Foreground="#FFFFFF" Padding="0,4,0,0"  TextAlignment="Center" Text="{Binding HeaderText, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
                        <Border Background="{StaticResource DellLightGrey}" TextBlock.Foreground="{StaticResource DarkestGrey}" Padding="8">
                            <ContentControl   Content="{Binding HelpContent, RelativeSource={RelativeSource Mode=TemplatedParent}}"   />
                        </Border>
                    </StackPanel>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

现在,您可以在我的模板中看到,我正在使用ContentControl来显示工具提示的内容。问题是,当我的HelpContent只是简单的字符串时,它不会对文本进行包装。我不能用ContentControl代替TextBlock,因为HelpContent也可以是其他类型(图像、丰富文本框等)。谁能告诉我解决这个问题的最好办法是什么?我会非常感激的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-17 13:04:28

ContentControl标记替换为:

代码语言:javascript
复制
<ContentPresenter Content="{TemplateBinding HelpContent}">
    <ContentPresenter.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="TextWrapping" Value="Wrap"/>
        </Style>
    </ContentPresenter.Resources>
</ContentPresenter>

注意:您可以将其保留为ContentControl,但是ContentPresenter更轻,并且遵循惯例

票数 8
EN

Stack Overflow用户

发布于 2014-04-17 13:05:44

StackPanel更改为Grid,因为它不知道要包装的宽度。

代码语言:javascript
复制
<Grid TextBlock.FontFamily="Trebuchet MS" TextBlock.FontSize='12'>
  <Grid.RowDefinitions>
     <RowDefinitions/>
     <RowDefinitions/>
  <Grid.RowDefinitions/>
  <TextBlock Grid.Row="0" Background="{StaticResource DellBlue}" Height="23" Foreground="#FFFFFF" Padding="0,4,0,0"  TextAlignment="Center" Text="{Binding HeaderText, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
  <Border Grid.Row="1" Background="{StaticResource DellLightGrey}" TextBlock.Foreground="{StaticResource DarkestGrey}" Padding="8">
    <ContentControl   Content="{Binding HelpContent, RelativeSource={RelativeSource Mode=TemplatedParent}}"   />
  </Border>
</Grid>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23133570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档