使用GetValue和SetValue来设置属性
GetValue和SetValue是在托管代码中直接获取或设置对象属性的一种方法,如下面例子:
<Rectangle x:Name="rect_MyRectangle" Width="100" Height="100" Fill="Blue"></Rectangle> <TextBlock x:Name="txb_MyText2" />
C#代码:
public yufa2() { InitializeComponent();//利用GetValue和SetValue double left = 400; double top = 30; double radius = 10; string width; string height; //设置矩形的位置 rect_MyRectangle.SetValue(Canvas.LeftProperty, left); rect_MyRectangle.SetValue(Canvas.TopProperty, top); //设置矩形的圆角度 rect_MyRectangle.SetValue(Rectangle.RadiusXProperty, radius); rect_MyRectangle.SetValue(Rectangle.RadiusYProperty, radius); //获取矩形的宽和高 width = rect_MyRectangle.GetValue(Rectangle.WidthProperty).ToString(); height = rect_MyRectangle.GetValue(Rectangle.HeightProperty).ToString(); //输出文本 txb_MyText2.Text = "矩形的宽是:" + width + " 高是:" + height; txb_MyText2.SetValue(Canvas.LeftProperty, 400.00); }
运行如图:
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· AI Agent爆火后,MCP协议为什么如此重要!
· Draw.io:你可能不知道的「白嫖级」图表绘制神器
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· Java使用多线程处理未知任务数方案