WPF 4.8与静态属性结合

发布于 2025-02-09 11:28:21 字数 697 浏览 2 评论 0原文

我创建ViewBox程序Maticaly。 如何将此控制程序绑定到非静态类的静态特性。

var bindingHeight = new Binding("viewbox_height");
        bindingHeight.Source = Config.viewbox_height;
        bindingHeight.Mode = BindingMode.TwoWay;
        bindingHeight.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        MyViewbox.SetBinding(Viewbox.HeightProperty, bindingHeight);


public class Config
{
    public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
    public static void OnPropertyChanged([CallerMemberName] string propertyname = null)
    {   
   StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyname));
    }

这样不起作用。

I create viewbox programmaticaly.
How to bind this control programaticaly to static property of non static class.

var bindingHeight = new Binding("viewbox_height");
        bindingHeight.Source = Config.viewbox_height;
        bindingHeight.Mode = BindingMode.TwoWay;
        bindingHeight.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        MyViewbox.SetBinding(Viewbox.HeightProperty, bindingHeight);


public class Config
{
    public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
    public static void OnPropertyChanged([CallerMemberName] string propertyname = null)
    {   
   StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyname));
    }

This way doesnt work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光沙漏 2025-02-16 11:28:21

将源设置为config的实例:

var bindingHeight = new Binding("viewbox_height");
bindingHeight.Source = new Config();
bindingHeight.Mode = BindingMode.TwoWay;
bindingHeight.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
MyViewbox.SetBinding(Viewbox.HeightProperty, bindingHeight);

Set the source to an instance of a Config:

var bindingHeight = new Binding("viewbox_height");
bindingHeight.Source = new Config();
bindingHeight.Mode = BindingMode.TwoWay;
bindingHeight.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
MyViewbox.SetBinding(Viewbox.HeightProperty, bindingHeight);
审判长 2025-02-16 11:28:21

至少您在设置binding.source时犯了一个错误。在通常的实例属性的情况下,它应该是具有该属性的对象,在您的情况下,是“ config”的实例。对于静态属性,您无需设置binding.source

At least you made a mistake in setting Binding.Source. In the case of usual instance property, it should be the object which has that property, in your case, the instance of "Config". In the case of static property, you don't need to set Binding.Source.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文