none
求高手指导DevExpress控件中Pie2DControl控件 RRS feed

  • 问题

  • 下载了一套DevExpress控件,看了软件的DEMO,我也想在自己的程序中加一个Pie2DControl控件。似乎有这么几个问题,请高手们给点指导。

    <local:ChartsDemoModule x:Class="ChartsDemo.Pie2DControl" Height="400" Width="800"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
        xmlns:dxdb="http://schemas.devexpress.com/winfx/2008/xaml/demobase"
        xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts"
        xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
        xmlns:local="clr-namespace:ChartsDemo"
        ModuleAppear="ChartsDemoModule_ModuleAppear">
        <local:ChartsDemoModule.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="/ChartsDemo;component/Themes/Generic.xaml" />
                </ResourceDictionary.MergedDictionaries>
                <local:DemoValuesProvider x:Key="DemoValuesProvider" />
                <local:Pie2DKindToPie2DModelConverter x:Key="Pie2DKindToPie2DModelConverter" />
            </ResourceDictionary>
        </local:ChartsDemoModule.Resources>
        <dxdb:DemoModuleControl>
            <dxdb:DemoModuleControl.OptionsContent>
                <StackPanel MinWidth="130">
                    <dxe:CheckEdit Name="chbVisible" Content="Show Labels" Margin="0,0,0,10" IsChecked="True" />
                    <dx:GroupFrame Header="Position" Margin="0,2,0,10" IsEnabled="{Binding ElementName=chbVisible, Path=IsChecked}">
                        <dxe:ListBoxEdit Name="lbPosition"
                                         Background="{x:Null}"
                                         ShowBorder="False"
                                         SelectedIndex="0"
                                         >
                            <dxc:PieLabelPosition>Outside</dxc:PieLabelPosition>
                            <dxc:PieLabelPosition>Inside</dxc:PieLabelPosition>
                            <dxe:ListBoxEdit.StyleSettings>
                                <dxe:RadioListBoxEditStyleSettings />
                            </dxe:ListBoxEdit.StyleSettings>
                        </dxe:ListBoxEdit>
                    </dx:GroupFrame>
                    <dx:GroupFrame Header="Hole Radius Percent" Margin="0,0,0,10">
                        <StackPanel>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Text="0" HorizontalAlignment="Left" />
                                <TextBlock Grid.Column="1" Text="50" HorizontalAlignment="Center" />
                                <TextBlock Grid.Column="2" Text="100" HorizontalAlignment="Right" />
                            </Grid>
                            <dxe:TrackBarEdit Name="slHoleRadiusPercent" Value="0" Minimum="0" Maximum="100">
                                <dxe:TrackBarEdit.TickPlacement>None</dxe:TrackBarEdit.TickPlacement>
                            </dxe:TrackBarEdit>
                        </StackPanel>
                    </dx:GroupFrame>
                    <dx:GroupFrame Header="Sweep Direction" Margin="0,0,0,10">
                        <dxe:ListBoxEdit Name="rblSweepDirection"
                                         Background="{x:Null}"
                                         ShowBorder="False"
                                         SelectedIndex="0"
                                         SelectedIndexChanged="rblSweepDirection_SelectedIndexChanged">
                            <dxc:PieSweepDirection>Counterclockwise</dxc:PieSweepDirection>
                            <dxc:PieSweepDirection>Clockwise</dxc:PieSweepDirection>
                            <dxe:ListBoxEdit.StyleSettings>
                                <dxe:RadioListBoxEditStyleSettings />
                            </dxe:ListBoxEdit.StyleSettings>
                        </dxe:ListBoxEdit>
                    </dx:GroupFrame>
                    <dx:GroupFrame Header="Rotation" Margin="0,0,0,10">
                        <StackPanel>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Text="-360" HorizontalAlignment="Left" />
                                <TextBlock Grid.Column="1" Text="0" HorizontalAlignment="Center" />
                                <TextBlock Grid.Column="2" Text="360" HorizontalAlignment="Right" />
                            </Grid>
                            <dxe:TrackBarEdit Name="slRotation" Value="0" Minimum="-360" Maximum="360">
                                <dxe:TrackBarEdit.TickPlacement>None</dxe:TrackBarEdit.TickPlacement>
                            </dxe:TrackBarEdit>
                        </StackPanel>
                    </dx:GroupFrame>
                    <dx:GroupFrame Header="Model" Margin="0,2,0,10">
                        <dxe:ListBoxEdit Name="lbModel"
                                         Background="{x:Null}"
                                         ShowBorder="False"
                                         ItemsSource="{Binding PredefinedPie2DKinds, Source={StaticResource DemoValuesProvider}}">
                            <dxe:ListBoxEdit.StyleSettings>
                                <dxe:RadioListBoxEditStyleSettings />
                            </dxe:ListBoxEdit.StyleSettings>
                        </dxe:ListBoxEdit>
                    </dx:GroupFrame>
                </StackPanel>
               
            </dxdb:DemoModuleControl.OptionsContent>
            <Grid>
                <dxc:ChartControl Name="chart" MouseUp="chart_MouseUp" MouseDown="chart_MouseDown" BorderThickness="0"
                                  QueryChartCursor="chart_QueryChartCursor" MouseMove="chart_MouseMove" EnableAnimation="True">
                    <dxc:ChartControl.Diagram>
                        <dxc:SimpleDiagram2D>
                            <dxc:SimpleDiagram2D.Series>
                                <dxc:PieSeries2D x:Name="Series" ArgumentScaleType="Qualitative" AnimationAutoStartMode="SetStartState"
                                                 HoleRadiusPercent="{Binding ElementName=slHoleRadiusPercent, Path=Value}"
                                                 Rotation="{Binding ElementName=slRotation, Path=Value}"
                                                 SweepDirection="{Binding ElementName=rblSweepDirection, Path=SelectedItem}"
                                                 Model="{Binding ElementName=lbModel, Path=SelectedItem, Converter={StaticResource Pie2DKindToPie2DModelConverter}}">
                                    <dxc:PieSeries2D.PointAnimation>
                                        <dxc:Pie2DFlyInAnimation Duration="0:0:1.5" PointOrder="Random" />
                                    </dxc:PieSeries2D.PointAnimation>
                                   
                                    <dxc:PieSeries2D.PointOptions>
                                        <dxc:PointOptions PointView="ArgumentAndValues">
                                            <dxc:PointOptions.ValueNumericOptions>
                                                <dxc:NumericOptions Format="Percent" Precision="0" />
                                            </dxc:PointOptions.ValueNumericOptions>
                                        </dxc:PointOptions>
                                    </dxc:PieSeries2D.PointOptions>
                                   
                                    <dxc:PieSeries2D.Label>
                                        <dxc:SeriesLabel Visible="{Binding ElementName=chbVisible, Path=IsChecked}"
                                                         dxc:PieSeries.LabelPosition="{Binding ElementName=lbPosition, Path=SelectedItem}"
                                                         RenderMode="RectangleConnectedToCenter"
                                                         />
                                    </dxc:PieSeries2D.Label>
                                   
                                    <dxc:PieSeries2D.Points>
                                        <dxc:SeriesPoint Argument="USA" Value="100" />
                                        <dxc:SeriesPoint Argument="Canada" Value="100" />
                                        <dxc:SeriesPoint Argument="Russia" Value="100" />
                                        <!--<dxc:SeriesPoint Argument="Others" Value="81.2" />-->
                                        <dxc:SeriesPoint Argument="India" Value="100" />
                                        <dxc:SeriesPoint Argument="Australia" Value="100" />
                                        <dxc:SeriesPoint Argument="Brazil" Value="100" />
                                        <dxc:SeriesPoint Argument="China" Value="100" />
                                    </dxc:PieSeries2D.Points>
                                </dxc:PieSeries2D>
                            </dxc:SimpleDiagram2D.Series>
                        </dxc:SimpleDiagram2D>
                    </dxc:ChartControl.Diagram>
                    <dxc:ChartControl.Titles>
                        <dxc:Title Dock="Top" HorizontalAlignment="Center" FontSize="24">Area of Countries</dxc:Title>
                        <dxc:Title Dock="Bottom" HorizontalAlignment="Right" FontSize="12" Foreground="Silver" Margin="0,10,0,0">From www.nationmaster.com</dxc:Title>
                    </dxc:ChartControl.Titles>
                </dxc:ChartControl>
            </Grid>
        </dxdb:DemoModuleControl>
    </local:ChartsDemoModule>

    后台代码:

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media.Animation;
    using DevExpress.Xpf.Charts;
    
    namespace ChartsDemo {
        public partial class Pie2DControl : ChartsDemoModule {
            const int clickDelta = 200;
            
            DateTime mouseDownTime;
            bool rotate;
            Point startPosition;
    
            public override ChartControl ActualChart { get { return chart; } }
    
            public Pie2DControl() {
                InitializeComponent();
                lbModel.SelectedItem = Pie2DModelKindHelper.FindActualPie2DModelKind(typeof(SimplePie2DModel));
            }
            bool IsClick(DateTime mouseUpTime) {
                return (mouseUpTime - mouseDownTime).TotalMilliseconds < clickDelta;
            }
            double CalcAngle(Point p1, Point p2) {
                Point center = new Point(chart.Diagram.ActualWidth / 2, chart.ActualHeight / 2);
                Point relativeP1 = new Point(p1.X - center.X, p1.Y - center.Y);
                Point relativeP2 = new Point(p2.X - center.X, p2.Y - center.Y);
                double angleP1Radian = Math.Atan2(relativeP1.X, relativeP1.Y);
                double angleP2Radian = Math.Atan2(relativeP2.X, relativeP2.Y);
                double angle = (angleP2Radian - angleP1Radian) * 180 / (Math.PI * 2);
                if (angle > 90)
                    angle = 180 - angle;
                else if (angle < -90)
                    angle = 180 + angle;
                return angle;
            }
            void chart_MouseUp(object sender, MouseButtonEventArgs e) {
                ChartHitInfo hitInfo = chart.CalcHitInfo(e.GetPosition(chart));
                rotate = false;
                if (hitInfo == null || hitInfo.SeriesPoint == null || !IsClick(DateTime.Now))
                    return;
                double distance = PieSeries.GetExplodedDistance(hitInfo.SeriesPoint);
                Storyboard storyBoard = new Storyboard();
                DoubleAnimation animation = new DoubleAnimation();
                animation.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 300));
                animation.To = distance > 0 ? 0 : 0.3;
                storyBoard.Children.Add(animation);
                Storyboard.SetTarget(animation, hitInfo.SeriesPoint);
                Storyboard.SetTargetProperty(animation, new PropertyPath(PieSeries.ExplodedDistanceProperty));
                storyBoard.Begin();
            }
            void chart_MouseDown(object sender, MouseButtonEventArgs e) {
                mouseDownTime = DateTime.Now;
                Point position = e.GetPosition(chart);
                ChartHitInfo hitInfo = chart.CalcHitInfo(position);
                if (hitInfo != null && hitInfo.SeriesPoint != null) {
                    rotate = true;
                    startPosition = position;
                }
            }
            void chart_MouseMove(object sender, MouseEventArgs e) {
                Point position = e.GetPosition(chart);
                ChartHitInfo hitInfo = chart.CalcHitInfo(position);
                if (hitInfo == null)
                    return;
                if (rotate && !IsClick(DateTime.Now)) {
                    PieSeries2D series = chart.Diagram.Series[0] as PieSeries2D;
                    double angleDelta = CalcAngle(startPosition, position);
                    if (Math.Abs(slRotation.Value + angleDelta) < 360)
                        slRotation.Value += angleDelta;
                    else if (slRotation.Value + angleDelta > 360)
                        slRotation.Value = -360;
                    else
                        slRotation.Value = 360;
                    startPosition = position;
                }
            }
            void ChartsDemoModule_ModuleAppear(object sender, RoutedEventArgs e) {
                chart.Animate();
            }
            void rblSweepDirection_SelectedIndexChanged(object sender, RoutedEventArgs e) {
                if (chart != null)
                    chart.Animate();
            }
            void chart_QueryChartCursor(object sender, QueryChartCursorEventArgs e) {
                ChartHitInfo hitInfo = chart.CalcHitInfo(e.Position);
                if (hitInfo != null && hitInfo.SeriesPoint != null)
                    e.Cursor = Cursors.Hand;
            }
        }
    }

    1.xaml中开始那一句<local:ChartsDemoModule x:Class="ChartsDemo.Pie2DControl" Height="400" Width="800" ;他这个应该是指本地后台代码的Pie2DControl的父类ChartsDemoModule还是什么意思?如果是要在<window></window>中使用这个控件,该怎么办呢?

    2.我照着他的DEMO自己做的时候,提示我少了引用控件,但是我添加了,还是不能自动识别<dxc:中控件名称。

    3.xaml中    ModuleAppear="ChartsDemoModule_ModuleAppear"  这前面这个ModuleAppear是什么意思?


    没有鞋子的人很多,但聪明的人会想到借,因为这样要比赤脚跑得快。



    • 已编辑 IItouch 2012年2月20日 11:20
    2012年2月20日 11:13

答案

全部回复

  • 1. 这个就相当于我们自己的一个UserControl, 你自己创建一个UserControl试试看,是不是XAML以<UserControl>开始,然后后台是你的class继承自UserControl。  要在窗体中使用,和使用自己的UserControl一样,首先你要做一次XML NameSpace mapping, xmlns:local="clr-namespace:ChartsDemo", 然后在窗体中用 <local:Pie2DControl />引用到你的Pie2DControl控件。

    2. dxc: 的名字空间映射到 "http://schemas.devexpress.com/winfx/2008/xaml/charts" ,所以你要确定你是否添加了程序集的引用。然后你千万别忘了你要和他一样做必要的XML NameSpace映射,xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts"

    3. 这个是 ChartsDemoModule 的一个属性,具体做什么看DevExpress的文档。

    这是个第三方的组件,微软不提供支持,我们也不可能马上有他的合法许可使用权,所以你如果还是有更多地DevExpress使用上的问题,我建议你到DevExpress官方技术支持站点咨询:

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    • 已标记为答案 IItouch 2012年2月21日 2:17
    2012年2月21日 2:05
    版主
  • 首先谢谢Bob,

    其实我也明白,我也映射了xml namespace的,也添加了引用,

    类也用同样的方法继承了,说白了,就是自己和他一样做控件。结果还是在dxc中没能智能感知到控件的存在。

    我在想想办法,试下。


    没有鞋子的人很多,但聪明的人会想到借,因为这样要比赤脚跑得快。

    2012年2月21日 2:17