You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UWP中WinUI 2.4 TeachingTip控件内容居中及隐藏关闭图标问题

解决WinUI 2.4 TeachingTip的内容居中和关闭图标隐藏问题

没问题,这两个需求都能轻松实现,我一步步给你拆解:

1. 让内容居中显示

你之前只给TextBlock设置了对齐,但TeachingTip的内容容器默认不会自动继承这个对齐逻辑。最简单的办法是直接给TeachingTip添加HorizontalContentAlignment="Center"和VerticalContentAlignment="Center"属性,这样内部内容就会自动居中。当然也可以把TextBlock嵌套在Grid里,利用Grid的布局特性实现居中,两种方式都可行,给你贴个完整示例:

<winui:TeachingTip x:Name="ToggleThemeTeachingTip2" 
                   IsOpen="True" 
                   PreferredPlacement="Bottom" 
                   PlacementMargin="30" 
                   Background="Green" 
                   Foreground="White"
                   HorizontalContentAlignment="Center"
                   VerticalContentAlignment="Center">
    <winui:TeachingTip.Content>
        <TextBlock Text="Content of the teaching tip"></TextBlock>
    </winui:TeachingTip.Content>
</winui:TeachingTip>

如果习惯用Grid容器的方式,代码是这样的(效果完全一致):

<winui:TeachingTip x:Name="ToggleThemeTeachingTip2" 
                   IsOpen="True" 
                   PreferredPlacement="Bottom" 
                   PlacementMargin="30" 
                   Background="Green" 
                   Foreground="White">
    <winui:TeachingTip.Content>
        <Grid>
            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="Content of the teaching tip"></TextBlock>
        </Grid>
    </winui:TeachingTip.Content>
</winui:TeachingTip>

2. 隐藏关闭图标

WinUI 2.4的TeachingTip自带了IsCloseButtonVisible属性,只要把它设为False,就能直接隐藏右上角的关闭按钮,非常省心。把这个属性加到你的控件里就行:

<winui:TeachingTip x:Name="ToggleThemeTeachingTip2" 
                   IsOpen="True" 
                   PreferredPlacement="Bottom" 
                   PlacementMargin="30" 
                   Background="Green" 
                   Foreground="White"
                   HorizontalContentAlignment="Center"
                   VerticalContentAlignment="Center"
                   IsCloseButtonVisible="False">
    <winui:TeachingTip.Content>
        <TextBlock Text="Content of the teaching tip"></TextBlock>
    </winui:TeachingTip.Content>
</winui:TeachingTip>

直接复制上面的完整代码就能满足你的两个需求啦~

内容的提问来源于stack exchange,提问作者sridhar patro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 13:22:33