1

我正在使用 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()正在调用该方法。

4

1 回答 1

2

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

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

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

于 2012-10-26T13:31:05.903 回答