WPF在C#代码中设置附加属性的值

来源:How to I access an attached property in code behind?

 

Q:

 

I have a rectangle in my XAML and want to change its Canvas.Left property in code behind:

 

复制代码
<UserControl x:Class="Second90.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300" KeyDown="txt_KeyDown">
    <Canvas>
        <Rectangle 
            Name="theObject" 
            Canvas.Top="20" 
            Canvas.Left="20" 
            Width="10" 
            Height="10" 
            Fill="Gray"/>
    </Canvas>
</UserControl>
复制代码

 

 

But this doesn't work:

 

private void txt_KeyDown(object sender, KeyEventArgs e)
{
    theObject.Canvas.Left = 50;
}

 

Does anyone know what the syntax is to do this?

A

Canvas.SetLeft(theObject, 50)

 

posted @   梦醒江南·Infinite  阅读(835)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
历史上的今天:
2019-04-06 Eclipse连接HBase 报错:org.apache.hadoop.hbase.PleaseHoldException: Master is initializing
2019-04-06 Eclipse Oxygen.2 Release (4.7.2)添加JUnit
点击右上角即可分享
微信分享提示