我正在使用Caliburn.Micro框架构建我的应用程序。我试图创建一个从WPF工具包图表派生的自定义图表控件,它需要添加2个自定义依赖项属性。Caliburn.Micro和自定义依赖项属性不起作用
由于某些原因,Caliburn.Micro未正确绑定到我创建的DP,但对现有的DP正常工作。我是否需要为CM识别这些附加属性?
(。在我的例子中,结合Title="{Binding ChartSeriesType}"
正常工作的ChartData
和ChartType
未更新)
SampleChart.xaml.cs
public partial class SampleChart : Chart
{
public ChartSeriesType ChartType
{
get { return (ChartSeriesType)GetValue(ChartTypeProperty); }
set
{
SetValue(ChartTypeProperty, value);
dataChaged();
}
}
// Using a DependencyProperty as the backing store for ChartType. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ChartTypeProperty =
DependencyProperty.Register("ChartType", typeof(ChartSeriesType), typeof(SampleChart), new UIPropertyMetadata(null));
public AgilityTableBase ChartData
{
get { return (AgilityTableBase)GetValue(ChartDataProperty); }
set
{
SetValue(ChartDataProperty, value);
dataChaged();
}
}
// Using a DependencyProperty as the backing store for ChartData. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ChartDataProperty =
DependencyProperty.Register("ChartData", typeof(AgilityTableBase), typeof(SampleChart), new UIPropertyMetadata(null));
private void dataChaged()
{
Console.WriteLine("data changed");
}
}
SampleChartView.xaml:
<UserControl x:Class="Agility.Presentation.ReportViewing.SampleChartView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:cal="http://www.caliburnproject.org"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Agility.Presentation.ReportViewing"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="300"
d:DesignWidth="300" mc:Ignorable="d">
<local:SampleChart Title="{Binding ChartSeriesType}" ChartData="{Binding ReportTable}" ChartType="{Binding ChartSeriesType}" />
S ampleChartViewModel.cs:
public class SampleChartViewModel : PropertyChangedBase
{
private ChartSeriesType _chartType;
private SampleTableBase _reportTable;
public SampleChartViewModel(SampleTableBase reportTable)
{
_reportTable = reportTable;
}
public SampleTableBase ReportTable
{
get { return _reportTable; }
set
{
_reportTable = value;
this.NotifyOfPropertyChange(() => ReportTable);
}
}
public ChartSeriesType ChartSeriesType
{
get { return _chartType; }
set
{
_chartType = value;
this.NotifyOfPropertyChange(() => ChartSeriesType);
}
}
}
编辑
注册ChartType DependencyProperty
是正确的方法:现在
// Using a DependencyProperty as the backing store for ChartType. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ChartTypeProperty =
DependencyProperty.Register("ChartType", typeof(ChartSeriesType), typeof(AgilityChart), new UIPropertyMetadata(ChartSeriesType.Bar
, new PropertyChangedCallback(dataChanged)));
private static void dataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Console.WriteLine("data changed");
}
,该dataChanged()
方法被调用。
我不太喜欢你在代码中使用这个属性的地方。正如我所看到的,您可以添加到一个图表,许多系列每个都有不同的类型和字符类型,什么都不做。 – Rafal
哎呀,我实际上并不想将'AddSeries'方法发布到这个问题上,它现在没有做任何事情。 – ChandlerPelhams