我正在使用 Caliburn.Micro 框架来构建我的应用程序。我正在尝试创建一个从 WPF 工具包图表派生的自定义图表控件,它需要添加 2 个自定义依赖项属性。
出于某种原因,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}" />
SampleChartViewModel.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()
正在调用该方法。