C# 自定义形状控件

C# 自定义形状控件,c#,custom-controls,microsoft-metro,C#,Custom Controls,Microsoft Metro,我想创建一个自定义控件以显示饼图。 我有一个PieSlice类(我从WinRT工具包项目中获得): 现在我正在尝试创建一个控件,它可以包含多个饼图切片 public class Pie : Control { #region Items Source public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register( "It

我想创建一个自定义控件以显示饼图。 我有一个PieSlice类(我从WinRT工具包项目中获得):

现在我正在尝试创建一个控件,它可以包含多个饼图切片

public class Pie : Control
{
    #region Items Source
    public static readonly DependencyProperty ItemsSourceProperty =
        DependencyProperty.Register(
            "ItemsSource",
            typeof(IEnumerable),
            typeof(Pie),
            new PropertyMetadata(
                null,
                new PropertyChangedCallback(OnItemsSourceChanged)));

    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    private static void OnItemsSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        var target = (Pie)sender;
        var oldItemsSource = (IEnumerable)e.OldValue;
        var newItemsSource = (IEnumerable)e.NewValue;
        target.OnItemsSourceChanged(oldItemsSource, newItemsSource);
    }

    private void OnItemsSourceChanged(IEnumerable oldItemsSource, IEnumerable newItemsSource)
    {
        UpdatePieSlices();
    }
    #endregion

    public Pie()
    {
        this.DefaultStyleKey = typeof(Pie);
    }

    private void UpdatePieSlices()
    {
        double startAngle = 0;
        foreach (KeyValuePair<string, double> item in ItemsSource)
        {
            PieSlice slice = new PieSlice() 
            { 
                Fill = new SolidColorBrush(Colors.Red), 
                Radius = 100, StartAngle = startAngle, 
                EndAngle = (item.Value / 100.0) * 360 
            };
            startAngle = (item.Value / 100.0) * 360;
        }
    }
}
公共类饼图:控件
{
#地区项目来源
公共静态只读依赖项Property ItemsSourceProperty=
从属属性。寄存器(
“项目资源”,
类型(IEnumerable),
类型(饼),
新属性元数据(
无效的
新资产变更回拨(OnItemSourceChanged));
公共IEnumerable ItemsSource
{
get{return(IEnumerable)GetValue(ItemsSourceProperty);}
set{SetValue(ItemsSourceProperty,value);}
}
私有静态资源已更改(DependencyObject发送方,DependencyPropertyChangedEventArgs e)
{
变量目标=(饼图)发送方;
var oldItemsSource=(IEnumerable)e.OldValue;
var newItemsSource=(IEnumerable)e.NewValue;
target.OnItemsSource已更改(旧项资源、新项资源);
}
私有资源已更改(IEnumerable oldItemsSource,IEnumerable newItemsSource)
{
UpdatePieSlices();
}
#端区
公众派()
{
this.DefaultStyleKey=typeof(饼图);
}
私有void更新许可证()
{
双星缠结=0;
foreach(ItemsSource中的KeyValuePair项)
{
PIESICE slice=新PIESICE()
{ 
填充=新的SolidColorBrush(颜色为红色),
半径=100,星形缠结=星形缠结,
端角=(item.Value/100.0)*360
};
startAngle=(item.Value/100.0)*360;
}
}
}
ItemsSource是
KeyValuePair
的集合,表示切片的名称和百分比。我想显示的切片,但我不知道如何

编辑:

我试过这个,但不起作用

<Style TargetType="control:Pie">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="control:Pie">
                    <Border
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">

                        <ItemsControl
                            AutomationProperties.AutomationId="ItemGridView"
                            AutomationProperties.Name="Grouped Items"
                            ItemsSource="{Binding Path=ItemsSource}">

                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <ContentControl Content="{Binding}"/>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <Grid></Grid>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

显示是控件默认外观的范围,在XAML中定义

我可能要做的是让控件公开一个
dependencProperty
,它是表示切片的对象集合。每个对象都将包含足够的信息来正确显示它所对应的切片,当
ItemsSource
更改时,控件的代码必须计算该切片

然后在XAML中,将其绑定到一个
ItemsControl
,该控件具有一个
DataTemplate
,该模板将切片描述对象绑定到实际的
PieSlice
对象,以及一个
ItemsPanelTemplate
,该模板可能只是一个
网格
Canvas
,以允许片段彼此堆积

您所做的是创建实际的
PieSlice
对象,这是可以的,但它们必须以不同的方式显示-您可以将它们的集合绑定到
ItemsControl
,该控件使用
ContentControl
作为其
itemstemplate
,将
内容
绑定到每个
PieSlice

<DataTemplate><ContentControl Content="{Binding}" /></DataTemplate>


关于为WPF和Silverlight创建自定义控件的信息将在这里很好地为您服务,因为WinRT中的基本思想和大部分技术都是相同的。

您能提供更多详细信息吗?我已经用xaml编辑了我的文章,但仍然没有work@Peekyou你能用你的最终工作方案编辑你的问题吗?谢谢
<DataTemplate><ContentControl Content="{Binding}" /></DataTemplate>