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

コードに見られる間違いの1つはdataChaged()、プロパティセッターに呼び出しを追加することです。このセッターはあなたにとって便利なだけです。バインディングによって呼び出されることはありません。依存関係プロパティの変更に対応するには、UIPropertyMetadatadpの登録時に渡すコールバックデリゲートを設定します。

さらに、コンストラクターの最初の引数はUIPropertyMetadataプロパティのデフォルト値でありChartSeriesType、列挙型のように見えるため、適切なデフォルトではnullになります。

次に、dp所有者タイプがに設定されているAgilityChart必要がSampleChartあります。これはクラス名であるためです。

于 2012-10-26T13:31:05.903 に答える