Caliburn.Micro和自定义依赖项属性不起作用

时间:2012-10-26 13:14:24

标签: wpf mvvm caliburn.micro

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

出于某种原因,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}" />

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

1 个答案:

答案 0 :(得分:2)

我在您的代码中看到的一个错误是您向属性设置器添加了dataChaged()调用。这个二传手只为您提供方便。它不会被绑定调用。要响应依赖项属性更改,请在注册dp时传递的UIPropertyMetadata中设置回调委托。

此外,UIPropertyMetadata构造函数的第一个参数是property的默认值,而ChartSeriesType看起来像一个枚举,因此在不合适的默认值中为null。

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