索性考虑用 ItemsControl 实现还比较方便些。

1. 前言

最近需要一个 WPF 的表盘控件,之前 Cyril-hcj 写过一篇不错的博客 《WPF在圆上画出刻度线》,里面介绍了一些原理及详细实现的代码:

double radius = BackEllipse.Width / 2;
double min = 0; double max = 100;
double step = 360.0 / (max - min);
for (int i = 0; i < max - min; i++)
{
    Line lineScale = new Line
    {
        X1 = ((radius - 20) * Math.Cos(i * step * Math.PI / 180)) + radius,
        Y1 = ((radius - 20) * Math.Sin(i * step * Math.PI / 180)) + radius,
        X2 = (radius * Math.Cos(i * step * Math.PI / 180)) + radius,
        Y2 = (radius * Math.Sin(i * step * Math.PI / 180)) + radius,
        Stroke = Brushes.Red,
        StrokeThickness = 2
    };

    MainCanvas.Children.Add(lineScale);
}

我本来想直接参考这篇文章的代码封装成一个控件,但我用得不多封装起来又麻烦,索性考虑用 ItemsControl 实现还比较方便些。

2. 使用 CirclePanel 实现

既然要用 ItemsControl,那首先要有个集合作为它的 ItemsSource。在 XAML 中可以用以下方式创建一个集合:

<x:Array x:Key="AuthorList" Type="{x:Type sys:String}">
    <sys:String>Mahesh Chand</sys:String>https://page.om.qq.com/page/OvB3zjsPln9pV5G6jqEnqOFQ0
    <sys:String>Praveen Kumar</sys:String>
    <sys:String>Raj Beniwal</sys:String>
    <sys:String>Neel Beniwal</sys:String>
    <sys:String>Sam Hobbs</sys:String>https://page.om.qq.com/page/OvB3zjsPln9pV5G6jqEnqOFQ0
</x:Array>https://page.om.qq.com/page/OvB3zjsPln9pV5G6jqEnqOFQ0

不过也可以不这么大费周章,在 .NET 中 string 也是一个集合,
可以用作 ItemsControl 的 ItemsSource。但在 Xaml 上直接写 ItemsSource="somestring"` 会报错,可以用 ContentControl 包装一下,https://page.om.qq.com/page/OvB3zjsPln9pV5G6jqEnqOFQ0?share_channel=6&#

posted @ 2022-08-31 12:30  星紫YXZ  阅读(28)  评论(0编辑  收藏  举报