执行时的WPF绑定

本文关键字:绑定 WPF 执行 | 更新日期: 2023-09-27 18:21:55

我的XAML文件中有以下绑定:

Fill="{Binding ElementName=cpRange1, Path=CurrentColor}"

在执行时设置同一个建筑的语法是什么?

执行时的WPF绑定

目前还不完全清楚您想要实现什么。如果您试图在运行时在代码后面的对象上设置绑定,您应该能够做到这一点:

对于给定的Rectangle

<Rectangle Name="MyRect"/>

在您的代码中:

        // Property to bind (example)....
        public SolidColorBrush MyColor { get; set; }
        //
        // In some initialisation method.
        MyColor = new SolidColorBrush(Colors.Blue);
        Binding myBinding = new Binding("MyColor");
        MyRect.SetBinding(Rectangle.FillProperty, myBinding);

在您的特定情况下,您可能希望设置myBinding.ElementName,并将myBinding.Path指向要作为目标的元素上的属性。

我可能误解了你的目标。

MSDN