【问题标题】:LiveCharts - DependencyProperty For Fill/Stroke Binding Not WorkingLiveCharts - 填充/描边绑定的 DependencyProperty 不起作用
【发布时间】:2018-05-14 22:52:07
【问题描述】:

我正在创建的可重用控件上的 DependencyProperty 存在问题,该控件使用 LiveCharts 绘制单线系列。问题是我要配置 3 个依赖属性;一个是图表的值,一个是系列的填充颜色,最后一个是线条系列的笔触颜色。这是我的 XAML:

<UserControl x:Class="DataAnalyzer.Controls.QuickPlotSingleLogFile2"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:DataAnalyzer.Controls"
         xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800"
         x:Name="parentControl">
<Grid x:Name="Grid_Container">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <lvc:CartesianChart Name="ChartFile" 
                        Grid.Row="0" 
                        LegendLocation="None" 
                        DisableAnimations="true" 
                        Hoverable="true" 
                        DataTooltip="{x:Null}" 
                        Margin="10" 
                        BorderBrush="Black">
        <lvc:CartesianChart.Series>
            <lvc:LineSeries x:Name="LineSeries1" 
                            PointGeometry="{x:Null}" 
                            Values="{Binding PlotValues}" 
                            Fill="{Binding FillBrush}" 
                            Stroke="{Binding StrokeBrush}" 
                            AreaLimit="0"></lvc:LineSeries>
        </lvc:CartesianChart.Series>
        <lvc:CartesianChart.AxisX>
            <lvc:Axis Labels=" " Title="Time">
                <lvc:Axis.Separator>
                    <lvc:Separator IsEnabled="False"></lvc:Separator>
                </lvc:Axis.Separator>
            </lvc:Axis>
        </lvc:CartesianChart.AxisX>
    </lvc:CartesianChart>
</Grid>

下面是代码:

public partial class QuickPlotSingleLogFile2 : UserControl, INotifyPropertyChanged
{

    // Formatter for the datetime in the x-axis for any series
    public Func<double, string> DateTimeSeriesFormatter { get; set; }

    #region PlotValues DP

    public ChartValues<double> PlotValues {
        get { return (ChartValues<double>)GetValue(PlotValuesProperty); }
        set { SetValue(PlotValuesProperty, value); }
        }

    public static readonly DependencyProperty PlotValuesProperty = DependencyProperty.Register("PlotValues", typeof(ChartValues<double>), typeof(QuickPlotSingleLogFile2));

    #endregion

    #region FillBrush DP

    public Brush FillBrush
    {
        get { return (Brush)GetValue(FillBrushProperty); }
        set { SetValue(FillBrushProperty, value); }
    }

    public static readonly DependencyProperty FillBrushProperty = DependencyProperty.Register("FillBrush", typeof(Brush), typeof(QuickPlotSingleLogFile2), new PropertyMetadata());

    #endregion

    #region StrokeBrush DP

    public Brush StrokeBrush
    {
        get { return (Brush)GetValue(StrokeBrushProperty); }
        set { SetValue(StrokeBrushProperty, value); }
    }

    public static readonly DependencyProperty StrokeBrushProperty = DependencyProperty.Register("StrokeBrush", typeof(Brush), typeof(QuickPlotSingleLogFile2), new PropertyMetadata(null));

    #endregion

    public QuickPlotSingleLogFile2()
    {
        InitializeComponent();

        Grid_Container.DataContext = this;

    }

    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;

     protected void OnPropertyChanged([CallerMemberName]string propertyName = null)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;

        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }
    #endregion

}

我的问题是,对于像“Fill”这样的属性,我是否还需要做更多的事情?我配置的 PlotValuesProperty 完全按预期工作——绑定没有问题。但我无法让绑定适用于填充或描边画笔 - 它会以某种方式丢失,并且实时图表提供填充和描边的默认值。此用户控件用于父窗口,数据上下文最终成为我想要的窗口。我检查了调试器以确保数据上下文设置正确,并且它似乎可以工作,因为图表的值设置正确。但是填充/描边发生了一些奇怪的事情。

【问题讨论】:

    标签: wpf dependency-properties livecharts


    【解决方案1】:

    我想出了这个问题的答案。我不明白为什么,但我没有看到绑定工作的原因是因为我没有在我的主窗口中初始化填充/描边属性。

    作为参考,我的主窗口原始代码(被截断以仅显示此自定义控件的相关绑定)是:

    public partial class MainWindow
    {
        #region Binding QuickPlotValues
        private ChartValues<double> _quickPlotSingleLogFileValues;
        public ChartValues<double> QuickPlotSingleLogFileValues
        {
            get
            {
                return _quickPlotSingleLogFileValues;
            }
            set
            {
                _quickPlotSingleLogFileValues = value;
                OnPropertyChanged("QuickPlotSingleLogFileValues");
            }
        }
        #endregion
    
        #region Binding QuickPlotFill
        private Brush _quickPlotFill;
        public Brush QuickPlotFill
        {
            get
            {
                return _quickPlotFill;
            }
            set
            {
                _quickPlotFill = value;
                OnPropertyChanged("QuickPlotFill");
            }
        }
        #endregion
    
        #region Binding QuickPlotStroke
        private Brush _quickPlotStroke;
        public Brush QuickPlotStroke
        {
            get
            {
                return _quickPlotStroke;
            }
            set
            {
                _quickPlotStroke = value;
                OnPropertyChanged("QuickPlotStroke");
            }
        }
        #endregion
    }
    

    自定义控件的 XAML 是:

    <vm:QuickPlotSingleLogFile2 x:Name="PreviewPlotSingleLogFile2"
                                                Grid.Row="1"
                                                Margin="20"
                                                VerticalAlignment="Stretch"
                                                MinHeight="250"
                                                PlotValues="{Binding QuickPlotSingleLogFileValues}"
                                                FillBrush="{Binding QuickPlotFill}"
                                                StrokeBrush="{Binding QuickPlotStroke}"/>
    

    我将主窗口代码更新为以下内容(初始化填充/描边颜色):

     #region Binding QuickPlotFill
        private Brush _quickPlotFill = new SolidColorBrush(Colors.Red);
        public Brush QuickPlotFill
        {
            get
            {
                return _quickPlotFill;
            }
            set
            {
                _quickPlotFill = value;
                OnPropertyChanged("QuickPlotFill");
            }
        }
        #endregion
    
        #region Binding QuickPlotStroke
        private Brush _quickPlotStroke = new SolidColorBrush(Colors.Green);
        public Brush QuickPlotStroke
        {
            get
            {
                return _quickPlotStroke;
            }
            set
            {
                _quickPlotStroke = value;
                OnPropertyChanged("QuickPlotStroke");
            }
        }
        #endregion
    

    突然间它奏效了。

    【讨论】:

    • 也许是个愚蠢的问题,但除了初始化之外,您是否设置过 QuickPlotFill 或 QuickPlotStroke 属性?你到底是在哪里做的?
    • 我在我的主窗口中执行此操作。我实际上有一个颜色选择器,可以让用户为特定系列分配颜色,所以我从存储的字典中提取用户选择的任何颜色。所以一旦它被初始化(我还是不明白为什么我需要这样做),我可以随时编写颜色,它会在幕后自动更新并重绘情节。
    猜你喜欢
    • 2011-12-25
    • 1970-01-01
    • 2012-04-15
    • 2014-03-29
    • 2015-10-02
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多