如何在Rectangle C#WPF中添加文本作为内容

 mobiledu2502907187 发布于 2023-02-07 16:19

我有以下矩形,填充颜色.我想在框中间添加一些文字.请提出一些解决方案.

var rect1 = new Rectangle
{
     Stroke = new SolidColorBrush(Colors.Red),
     Fill = new SolidColorBrush(Colors.Black),
     Width = 150,
     Height = 100,
     VerticalAlignment = System.Windows.VerticalAlignment.Top,
     HorizontalAlignment = System.Windows.HorizontalAlignment.Left
 };
 Grid.SetRow(rect, 0);
 TGrid2.Children.Add(rect1);

基本上我想做一个拖放功能.其中rect1将被拖到rect2上; 只有边框颜色和透明的Fill-body和on drop用rect1替换rect2.这就是为什么我做了一个矩形,现在试图想象如何在矩形中添加文本作为中心内容,类似于按钮.

2 个回答
  • 替换为Rectanglea Grid或a Border并放入TextBlock内部.

    2023-02-07 16:21 回答
  • 如果你绝对必须使用一个矩形,你可以伪造它!

    <Grid>
        <Rectangle Fill="Red"/>
        <TextBlock Text="Hi there" Margin="5"/>
    </Grid>
    

    2023-02-07 16:21 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有