实时图表 - 填充/描边绑定的依赖项属性不起作用



我正在创建的可重用控件上的依赖项属性出现问题,该控件使用 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
}

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

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

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

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

突然间,它奏效了。

相关内容

  • 没有找到相关文章