我在可重复使用的控件上遇到了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
}
我的问题是,如果有更多我需要做的事情,例如&#34; Fill&#34;?我配置的PlotValuesProperty完全按预期工作 - 绑定没有问题。但是我无法使用绑定工作填充或笔画 - 它会以某种方式丢失,而实时图表会为填充和描边提供默认值。这个用户控件在父窗口中使用,数据上下文最终成为窗口,这就是我想要的。我检查了调试器以确保正确设置了数据上下文,并且它似乎正常工作,因为图表的值设置正确。但填充/描边发生了奇怪的事情。
答案 0 :(得分: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
突然间它奏效了。