2012-10-26 41 views
1

我正在使用Caliburn.Micro框架构建我的应用程序。我试图创建一个从WPF工具包图表派生的自定义图表控件,它需要添加2个自定义依赖项属性。Caliburn.Micro和自定义依赖项属性不起作用

由于某些原因,Caliburn.Micro未正确绑定到我创建的DP,但对现有的DP正常工作。我是否需要为CM识别这些附加属性?

(。在我的例子中,结合Title="{Binding ChartSeriesType}"正常工作的ChartDataChartType未更新)

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()方法被调用。

+0

我不太喜欢你在代码中使用这个属性的地方。正如我所看到的,您可以添加到一个图表,许多系列每个都有不同的类型和字符类型,什么都不做。 – Rafal

+0

哎呀,我实际上并不想将'AddSeries'方法发布到这个问题上,它现在没有做任何事情。 – ChandlerPelhams

回答

2

我在你的代码中看到的一个错误是你给你的属性setter添加了dataChaged()调用。这个二传手对你来说只是一个方便。它不会被绑定调用。要对注册dp时通过的UIPropertyMetadata中的依赖项属性更改设置回调代理作出响应。

此外,UIPropertyMetadata构造函数的第一个参数是属性的默认值,而ChartSeriesType看起来像一个枚举,因此在不适当的默认情况下为null。

接下来的事情是,dp所有者类型设置为AgilityChart,应该是SampleChart,因为那是您的类名称。

+0

哎呀!我仍然是WPF的新手,所以请耐心等待。导致我认为它不能正常工作的错误不是设置“PropertyChangedCallback”。谢谢您的帮助! – ChandlerPelhams

相关问题