c# - 在代码后面更改 Canvas.Left 属性?

标签 c# wpf code-behind attached-properties

我的 XAML 中有一个矩形,我想在后面的代码中更改它的 Canvas.Left 属性:

<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>

但这行不通:

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

有谁知道这样做的语法是什么?

最佳答案

Canvas.SetLeft(theObject, 50)

关于c# - 在代码后面更改 Canvas.Left 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/541420/

相关文章:

c# - 添加 using 语句或编写 namespace.classname 方法有什么区别

c# - pInvoke C# DLL导入问题

c# - 通过C#应用程序访问西门子S7-1200

c# - 将 FlowDocument 绑定(bind)到我的 FlowDocumentScrollViewer

.net - SplashScreen.Close() 窃取 MainWindow 的焦点

asp.net - 在带有属性的代码隐藏中添加 cssstyle

c# - 我如何获得一个月的最后一天?

c# - 由组合框组成的非常简单的颜色选择器

c# - 在 ASP.NET 的代码隐藏中读取动态生成的 HTML 元素值

wpf - ResourceDictionary 添加 x :Class