我在将颜色转换为十六进制时遇到问题。下面有红色下划线System.Drawing.ColorTranslator.FromHtml("paint")
and rect.Color;
多变的paint
是静态的 - 目前。
在我看来,问题在于变量的类型 publicSystem.Drawing.SolidBrush Color
at Rect
class
List<Rect> rects = new List<Rect>();
rects.Add(new Rect()
{
Width = x,
Height = y,
Left = w,
Top = h,
Fill = (System.Windows.Media.Brush)(new BrushConverter()).ConvertFromString(paint)
});
foreach (Rect rect in rects)
{
Rectangle r = new Rectangle
{
Width = rect.Width,
Height = rect.Width,
Fill = rect.Fill
};
Canvas.SetLeft(r, rect.Left);
Canvas.SetTop(r, rect.Top);
canvas.Children.Add(r);
}
}
class Rect
{
public int Width { get; set; }
public int Height { get; set; }
public int Left { get; set; }
public int Top { get; set; }
public System.Windows.Media.Brush Fill { get; set; }
}
private void rectangle_Click(object sender, RoutedEventArgs e)
{
choose r1 = new choose();
var paint = "#FFA669D1";
int x = int.Parse(beginx.Text);
int y = int.Parse(beginy.Text);
int w = int.Parse(wid.Text);
int h = int.Parse(hei.Text);
if (!((x > canvas.ActualWidth) || (y > canvas.ActualHeight) || (w > canvas.ActualWidth) || (h > canvas.ActualHeight)))
{
r1.rectangle(x, y, w, h, paint, canvas);
}
}
不要使用不兼容的 WinForms 类型System.Drawing.SolidBrush
为了Fill
WPF 矩形的属性。使用System.Windows.Media.Brush
反而:
class Rect
{
...
public Brush Fill { get; set; }
}
然后使用WPFBrushConverter
将十六进制颜色字符串转换为 Brush 的类:
rect.Fill = (Brush)(new BrushConverter()).ConvertFromString(paint);
在您的代码示例中,它应该如下所示:
var converter = new BrushConverter();
rects.Add(new Rect
{
Width = x,
Height = y,
Left = w,
Top = h,
Fill = (Brush)converter.ConvertFromString(paint)
});
foreach (Rect rect in rects)
{
Rectangle r = new Rectangle
{
Width = rect.Width,
Height = rect.Width,
Fill = rect.Fill
};
Canvas.SetLeft(r, rect.Left);
Canvas.SetTop(r, rect.Top);
canvas.Children.Add(r);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)