Wpf LiveCharts-填充/笔划绑定的DependencyProperty不起作用
我正在创建一个可重用控件,该控件使用LiveCharts绘制单线系列,但该控件的DependencyProperty存在问题。问题是我有3个要配置的依赖属性;一个是图表的值,一个是序列的填充颜色,最后一个是直线序列的笔划颜色。这是我的XAML:Wpf LiveCharts-填充/笔划绑定的DependencyProperty不起作用,wpf,dependency-properties,livecharts,Wpf,Dependency Properties,Livecharts,我正在创建一个可重用控件,该控件使用LiveCharts绘制单线系列,但该控件的DependencyProperty存在问题。问题是我有3个要配置的依赖属性;一个是图表的值,一个是序列的填充颜色,最后一个是直线序列的笔划颜色。这是我的XAML: <UserControl x:Class="DataAnalyzer.Controls.QuickPlotSingleLogFile2" xmlns="http://schemas.microsoft.com/winfx/2006
<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
}
公共部分类QuickPlotSingleLogFile2:UserControl,INotifyPropertyChanged
{
//任何系列的x轴上日期时间的格式化程序
public Func datetimeseriesformter{get;set;}
#区域值
公共图表值{
获取{return(ChartValues)GetValue(PlotValuesProperty);}
set{SetValue(PlotValuesProperty,value);}
}
公共静态只读DependencyProperty PlotValuesProperty=DependencyProperty.Register(“PlotValues”、typeof(ChartValues)、typeof(QuickPlotSingleLogFile2));
#端区
#区域填充刷DP
公共灌木丛
{
获取{return(画笔)GetValue(FillBrushProperty);}
set{SetValue(FillBrushProperty,value);}
}
公共静态只读DependencyProperty FillBrushProperty=DependencyProperty.Register(“FillBrush”、typeof(Brush)、typeof(QuickPlotSingleLogFile2)、new PropertyMetadata());
#端区
#区域频闪刷
公共刷子
{
获取{return(Brush)GetValue(strokeBushProperty);}
set{SetValue(StrokeBrushProperty,value);}
}
公共静态只读DependencyProperty StrokeBrushProperty=DependencyProperty.Register(“StrokeBrush”、typeof(Brush)、typeof(QuickPlotSingleLogFile2)、new PropertyMetadata(null));
#端区
公共QuickPlotSingleLogFile2()
{
初始化组件();
Grid_Container.DataContext=this;
}
#区域INotifyProperty更改成员
公共事件属性更改事件处理程序属性更改;
受保护的OnPropertyChanged无效([CallerMemberName]字符串propertyName=null)
{
PropertyChangedEventHandler处理程序=this.PropertyChanged;
if(处理程序!=null)
{
var e=新的PropertyChangedEventArgs(propertyName);
处理者(本,e);
}
}
#端区
}
我的问题是,对于“填充”这样的属性,我是否还需要做更多的事情?我配置的PlotValuesProperty完全按照预期工作——绑定没有问题。但是我无法使fill或stroke笔刷的绑定工作——它不知何故丢失了,而livecharts为fill和stroke提供了默认值。这个用户控件在父窗口中使用,数据上下文最终成为窗口,这就是我想要的。我已经检查了调试器,以确保数据上下文设置正确,并且由于图表的值设置正确,因此它似乎可以工作。但填充/笔划出现了一些奇怪的情况 我找到了这个问题的答案。我不知道确切的原因,但我没有看到绑定工作的原因是因为我没有在主窗口中初始化填充/笔划属性 作为参考,我的主窗口原始代码(截断为仅显示此自定义控件的相关绑定)为:
突然,它起了作用 也许是个愚蠢的问题,但除了初始化之外,您是否设置过QuickPlotFill或QuickPlotStroke属性?你到底在哪里做的?我在我的主窗口做的。实际上,我有一个颜色选择器,允许用户为特定系列指定颜色,因此我从存储的字典中提取用户选择的任何颜色。因此,一旦它被初始化(我也不明白为什么我需要这样做),我可以随时编写颜色,它会在幕后自动更新并重新绘制情节。
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
}
<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