Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF椭圆在MouseEnter事件中无法正确调整大小_C#_Wpf - Fatal编程技术网

C# WPF椭圆在MouseEnter事件中无法正确调整大小

C# WPF椭圆在MouseEnter事件中无法正确调整大小,c#,wpf,C#,Wpf,我正在使用ChartView Telerik WPF库。我希望当用户将鼠标悬停在这些点上时,这些点会变得更大。但由于某些原因,它并没有像预期的那样发挥作用。椭圆会变大,但无法正确调整大小。但我不明白为什么。其他属性(如边框颜色和厚度)工作正常。 有人能告诉我我错过了什么吗 这是当前我尝试调整点大小时的外观: 以下是源代码: private FrameworkElementFactory AddPointsToSeries(KeyValuePair<ChartSerie, List<

我正在使用ChartView Telerik WPF库。我希望当用户将鼠标悬停在这些点上时,这些点会变得更大。但由于某些原因,它并没有像预期的那样发挥作用。椭圆会变大,但无法正确调整大小。但我不明白为什么。其他属性(如边框颜色和厚度)工作正常。 有人能告诉我我错过了什么吗

这是当前我尝试调整点大小时的外观:

以下是源代码:

private FrameworkElementFactory AddPointsToSeries(KeyValuePair<ChartSerie, List<ChartDataPoint>> chartSeries, int colorPaletteIndex)
    {
        var seriesPredefinedColor = this.ChartBase.Palette.GlobalEntries[colorPaletteIndex].Fill;

        FrameworkElementFactory frameworkElement = new FrameworkElementFactory(typeof(Ellipse));
        frameworkElement.SetValue(Ellipse.FillProperty, ColorService.BrushFromHex(chartSeries.Key.ColorHex) ?? seriesPredefinedColor);
        frameworkElement.SetValue(Ellipse.HeightProperty, 9.0D);
        frameworkElement.SetValue(Ellipse.WidthProperty, 9.0D);


        frameworkElement.AddHandler(Ellipse.MouseEnterEvent, new MouseEventHandler((sender, args) =>
           {
               Ellipse ellipse = (Ellipse)sender;
               ellipse.Stroke = ColorService.BrushFromHex(ColorService.BlendHex((chartSeries.Key.ColorHex ?? ColorService.BrushToHex(seriesPredefinedColor)), "#000000", 0.4));

               // this is not correctly applied!
               ellipse.Width = 15;
               ellipse.Height = 15;

               ellipse.StrokeThickness = 2;
           }));

        frameworkElement.AddHandler(Ellipse.MouseLeaveEvent, new MouseEventHandler((sender, args) =>
           {
               Ellipse ellipse = (Ellipse)sender;
               ellipse.Height = 8;
               ellipse.Width  = 8;

               ellipse.Stroke = null;
           }));


        return frameworkElement;
    }

    // Here I create the Line Series and here I use the AddPointsToSeries Method
    private LineSeries CreateLineSeries(KeyValuePair<ChartSerie, List<ChartDataPoint>> chartSeries, ChartLegendSettings legendSettings,
                                        int colorPaletteIndex)
    {
        FrameworkElementFactory addPoints = AddPointsToSeries(chartSeries, colorPaletteIndex);
        var lineSerie = new LineSeries()
        {
            VerticalAxis    = CreateMultipleVerticalAxis(chartSeries, colorPaletteIndex, out var multipleVerticalAxis) ? multipleVerticalAxis : null,
            ZIndex          = 150, // the line series should always be displayed on top of other series.
            StrokeThickness = 3.5,
            LegendSettings  = (SeriesLegendSettings)legendSettings,
            Opacity         = 0.8,
            StackGroupKey = chartSeries.Key.Group,
            CombineMode   = string.IsNullOrEmpty(chartSeries.Key.Group) ? ChartSeriesCombineMode.None : ChartSeriesCombineMode.Stack,

            PointTemplate = new DataTemplate()
            {
                VisualTree = addPoints,
            },
        };

        // this is the color of line series
        if (chartSeries.Key.ColorHex != null)
        {
            lineSerie.Stroke = (SolidColorBrush)(new BrushConverter().ConvertFrom(chartSeries.Key.ColorHex));
        }

        foreach (ChartDataPoint serie in chartSeries.Value)
        {
            lineSerie.DataPoints.Add(new CategoricalDataPoint()
            {
                Category = serie.XPoint.Label,
                Value    = (double?)serie.Value,
            });
        }

        return lineSerie;
    }
private frameworkelement工厂AddPointsToSeries(KeyValuePair图表系列,int colorpleteIndex)
{
var seriesPredefinedColor=this.ChartBase.palete.GlobalEntries[colorPaletteIndex].Fill;
frameworkElement工厂frameworkElement=新的frameworkElement工厂(类型为(椭圆));
frameworkElement.SetValue(Ellipse.FillProperty,ColorService.BrushFromHex(chartSeries.Key.ColorHex)??SeriesPreferenceColor);
frameworkElement.SetValue(Ellipse.HeightProperty,9.0D);
frameworkElement.SetValue(Ellipse.WidthProperty,9.0D);
frameworkElement.AddHandler(Ellipse.MouseEnterEvent,新的MouseEventHandler((发送方,参数)=>
{
椭圆=(椭圆)发送器;
椭圆.Stroke=ColorService.BrushFromHex(ColorService.BlendHex((chartSeries.Key.ColorHex??ColorService.BrushToHex(seriesPreferencedColor)),“000000”,0.4));
//这是不正确的应用!
椭圆。宽度=15;
椭圆。高度=15;
椭圆.StrokeThickness=2;
}));
frameworkElement.AddHandler(Ellipse.MouseLeaveEvent,新的MouseeEventHandler((发送方,参数)=>
{
椭圆=(椭圆)发送器;
椭圆,高度=8;
椭圆。宽度=8;
椭圆。笔划=空;
}));
返回框架元素;
}
//这里我创建了线系列,这里我使用AddPointsToSeries方法
专用LineSeries CreateLineSeries(KeyValuePair chartSeries、ChartLegendSettings legendSettings、,
int颜色调色板索引)
{
FrameworkElementFactory addPoints=AddPointsToSeries(chartSeries,colorPaletteIndex);
var lineSerie=new LineSeries()
{
VerticalAxis=CreateMultipleVerticalAxis(chartSeries、colorPaletteIndex、out var multipleVerticalAxis)?multipleVerticalAxis:null,
ZIndex=150,//直线系列应始终显示在其他系列的顶部。
冲程厚度=3.5,
LegendSettings=(系列LegendSettings)LegendSettings,
不透明度=0.8,
StackGroupKey=chartSeries.Key.Group,
CombineMode=string.IsNullOrEmpty(chartSeries.Key.Group)?ChartSeriesCombineMode.None:ChartSeriesCombineMode.Stack,
PointTemplate=新数据模板()
{
VisualTree=添加点,
},
};
//这是线条系列的颜色
if(chartSeries.Key.ColorHex!=null)
{
lineSerie.Stroke=(SolidColorBrush)(新的BrushConverter().ConvertFrom(chartSeries.Key.ColorHex));
}
foreach(chartSeries.Value中的ChartDataPoint系列)
{
lineSerie.DataPoints.Add(新分类数据点()
{
类别=serie.XPoint.Label,
值=(双?)系列值,
});
}
回程线系列;
}

我的问题得到了马丁·伊万诺夫的回答:

此行为来自图表的大小缓存机制。基本上,控件是在最初加载时设置视觉对象的大小,然后它不会更改它,除非图表的API发生更改。为了达到您的要求,您可以将椭圆包裹在网格面板中,其宽度和高度属性与较大椭圆的大小相同(或稍大)

这就是解决方案:

private FrameworkElementFactory AddPointsToSeries(KeyValuePair<ChartSerie, List<ChartDataPoint>> chartSeries, int colorPaletteIndex)
    {
        var   seriesPredefinedColor = this.ChartBase.Palette.GlobalEntries[colorPaletteIndex].Fill;
        Brush brush                 = chartSeries.Key.ColorHex == null ? (seriesPredefinedColor) : ColorService.HexToBrush(chartSeries.Key.ColorHex);
        Brush mouseOnEnterColor     = new SolidColorBrush(ColorService.ChangeColorLightness(ColorService.BrushToColor(brush), 0.8));

        double ellipseMouseOverStrokeThickness = 2;
        double ellipseMouseOverHeightWidth     = 13;
        double ellipseStrokeThickness          = 1;
        double ellipseHeightWidth              = 9;

        FrameworkElementFactory frameworkElement = new FrameworkElementFactory(typeof(Ellipse));
        frameworkElement.SetValue(Ellipse.FillProperty, brush);
        frameworkElement.SetValue(Ellipse.MarginProperty, new Thickness(-4.5));
        frameworkElement.SetValue(Ellipse.HeightProperty, ellipseHeightWidth);
        frameworkElement.SetValue(Ellipse.WidthProperty, ellipseHeightWidth);
        frameworkElement.SetValue(Ellipse.StrokeProperty, new SolidColorBrush(Colors.White));
        frameworkElement.SetValue(Ellipse.StrokeThicknessProperty, ellipseStrokeThickness);


        frameworkElement.AddHandler(Ellipse.MouseEnterEvent, new MouseEventHandler((sender, args) =>
                                                                                   {
                                                                                       Ellipse ellipse = (Ellipse)sender;
                                                                                       ellipse.Fill            = new SolidColorBrush(Colors.White);
                                                                                       ellipse.Stroke          = mouseOnEnterColor;
                                                                                       ellipse.StrokeThickness = ellipseMouseOverStrokeThickness;
                                                                                       ellipse.Width           = ellipseMouseOverHeightWidth;
                                                                                       ellipse.Height          = ellipseMouseOverHeightWidth;
                                                                                   }));

        frameworkElement.AddHandler(Ellipse.MouseLeaveEvent, new MouseEventHandler((sender, args) =>
                                                                                   {
                                                                                       Ellipse ellipse = (Ellipse)sender;
                                                                                       ellipse.Stroke          = new SolidColorBrush(Colors.White);
                                                                                       ellipse.Fill            = brush;
                                                                                       ellipse.StrokeThickness = ellipseStrokeThickness;
                                                                                       ellipse.Height          = ellipseHeightWidth;
                                                                                       ellipse.Width           = ellipseHeightWidth;
                                                                                   }));

        FrameworkElementFactory stackPanelFactory = new FrameworkElementFactory(typeof(Grid));
        stackPanelFactory.SetValue(Grid.HeightProperty, ellipseMouseOverHeightWidth + ellipseMouseOverStrokeThickness);
        stackPanelFactory.SetValue(Grid.WidthProperty, ellipseMouseOverHeightWidth  + ellipseMouseOverStrokeThickness);
        stackPanelFactory.AppendChild(frameworkElement);

        return stackPanelFactory;
    }
private frameworkelement工厂AddPointsToSeries(KeyValuePair图表系列,int colorpleteIndex)
{
var seriesPredefinedColor=this.ChartBase.palete.GlobalEntries[colorPaletteIndex].Fill;
画笔画笔=chartSeries.Key.ColorHex==null?(seriesPreferencedColor):ColorService.HexToBrush(chartSeries.Key.ColorHex);
Brush mouseOnEnterColor=新的SolidColorBrush(ColorService.ChangeColorLightness(ColorService.BrushToColor(Brush),0.8));
双椭圆记忆超行程厚度=2;
双椭圆符号超高宽度=13;
双椭圆型壁厚=1;
双椭圆宽度=9;
frameworkElement工厂frameworkElement=新的frameworkElement工厂(类型为(椭圆));
frameworkElement.SetValue(Ellipse.FillProperty,笔刷);
frameworkElement.SetValue(椭圆.MarginProperty,新厚度(-4.5));
frameworkElement.SetValue(Ellipse.HeightProperty,ellipseHeightWidth);
frameworkElement.SetValue(Ellipse.WidthProperty,ellipseHeightWidth);
frameworkElement.SetValue(Ellipse.StrokeProperty,新的SolidColorBrush(Colors.White));
frameworkElement.SetValue(Ellipse.StrokeThicknessProperty,ellipseStrokeThickness);
frameworkElement.AddHandler(Ellipse.MouseEnterEvent,新的MouseEventHandler((发送方,参数)=>
{
椭圆=(椭圆)发送器;
ellipse.Fill=新的SolidColorBrush(Colors.White);
椭圆。笔划=鼠标输入颜色;