将毛伊岛控制背景色绑定到具有数据类型系统的属性。drawing.Color

发布于 2025-01-29 06:28:58 字数 905 浏览 2 评论 0原文

我有一个带有颜色属性的类,该类别是数据类型system.drawing.color,它用于绑定Windows表单控件,我想将MAUI控制背景色绑定到它。毛伊岛控制背景颜色的数据类型是microsoft.maui.graphics.color。当我尝试将毛伊岛控制直接绑定到颜色时,控制背景颜色只是灰色。这是因为数据类型不同而被理解的。我尝试将属性添加到将颜色返回字符串的类中,我对此的想法是,绑定以颜色名称为字符串,因此它应该能够绑定到字符串属性。它也没有起作用。

//class properties
public Color ShapeColor
        {
            get {
                return colorval;
            }
            set
            {
                colorval = value;
                InvokePropertyChanged();
                InvokePropertyChanged("ShapeColorToString");
            }
        }
    
public string ShapeColorToString { get { return this.ShapeColor.ToString(); } }

以下与颜色绑定不起作用:

<Button x:Name="btn1"BackgroundColor="{Binding Path=ShapeColor}" />

颜色也没有像字符串一样绑定:

<Button x:Name="btn2" BackgroundColor="{Binding Path=ShapeColorToString}"/>

I have a class with a Color property that is data type System.Drawing.Color, it is used for binding a Windows Forms control, and I want to bind a MAUI control BackgroundColor to it. The data type for the MAUI control background color is Microsoft.Maui.Graphics.Color. When I try binding the MAUI control straight to the Color, the control background color is just gray. That is understood because the data types are different. I tried adding a property to the class which returns the color as a string, my thinking on that was that the Binding takes a color name as a string, so it should be able to bind to a string property. It did not work either.

//class properties
public Color ShapeColor
        {
            get {
                return colorval;
            }
            set
            {
                colorval = value;
                InvokePropertyChanged();
                InvokePropertyChanged("ShapeColorToString");
            }
        }
    
public string ShapeColorToString { get { return this.ShapeColor.ToString(); } }

the following bound to Color did not work:

<Button x:Name="btn1"BackgroundColor="{Binding Path=ShapeColor}" />

nor did binding to color as string work:

<Button x:Name="btn2" BackgroundColor="{Binding Path=ShapeColorToString}"/>

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

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

发布评论

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

评论(1

年华零落成诗 2025-02-05 06:28:58

到目前为止,我拥有的最好的解决方案是:将Microsoft.maui.graphics安装到类库中(当前预览,因此请确保您在Nuget浏览中包含Preview)。然后添加一个将system.color转换为microsoft.maui.graphics.color的属性:

public Microsoft.Maui.Graphics.Color ShapeColorForMAUI { 
get => Microsoft.Maui.Graphics.Color.FromRgb(this.ShapeColor.R, this.ShapeColor.G, this.ShapeColor.B); 
}

我宁愿不在类库中安装Maui Graphics,但这是我能想到的最好的。

The best solution I have so far is: Install Microsoft.Maui.Graphics into the class library (currently preview, so ensure that you include preview in the NuGet browse). And then add a property that converts System.Drawing.Color to Microsoft.Maui.Graphics.Color as so:

public Microsoft.Maui.Graphics.Color ShapeColorForMAUI { 
get => Microsoft.Maui.Graphics.Color.FromRgb(this.ShapeColor.R, this.ShapeColor.G, this.ShapeColor.B); 
}

I would rather not install MAUI graphics in the class library, but that is the best I can come up with.

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