3

2 つの ObservableCollection< TimeValue > を持つクラスがあります。ここで、TimeValue は、(INotifyPropertyChanged を介して) 変更通知と組み合わせたカスタムの DateTime/Value です。私はこれらを目標と実績と呼んでいます。

これらをチャートにバインドすると、すべてが完全に機能し、2 つの LineSeries が得られます。「日付」の列と「値」の列を使用して、それらの1つをDataGridにバインドすると、再び完全に機能します。必要な TwoWay バインディングも取得します。

ただし、「日付」列と、ターゲットと実績のそれぞれの列を持つ DataGrid が必要です。問題は、範囲内のすべての日付をリストする必要があることですが、これらの日付の一部には、ターゲット、実績、またはその両方に対応する値がない場合があります。

そこで、Targets と Actuals を入力として取得し、結合された TimeSeriesC を出力する MultiBinding を実行することにしました。元の値のいずれかに値がない場合は常に null 値を使用します。

機能しますが、基になるデータの変更には応答しません。

これは正常に動作します (1 つの ObservableCollection にバインド):

<ctrls:DataGrid Grid.Row="1" Height="400" AutoGenerateColumns="False" CanUserDeleteRows="False" SelectionUnit="Cell">
<ctrls:DataGrid.ItemsSource>
    <Binding Path="Targets"/>
    <!--<MultiBinding Converter="{StaticResource TargetActualListConverter}">
        <Binding Path="Targets"/>
        <Binding Path="Actuals"/>
    </MultiBinding>-->
</ctrls:DataGrid.ItemsSource>
<ctrls:DataGrid.Columns>
    <ctrls:DataGridTextColumn Header="Date" Binding="{Binding Date,StringFormat={}{0:ddd, MMM d}}"/>
    <ctrls:DataGridTextColumn Header="Target" Binding="{Binding Value}"/>
    <!--<ctrls:DataGridTextColumn Header="Target" Binding="{Binding Value[0]}"/>
    <ctrls:DataGridTextColumn Header="Actual" Binding="{Binding Value[1]}"/>-->
</ctrls:DataGrid.Columns>

これは機能しますが、最初の初期化時のみです。変更通知への応答なし:

<ctrls:DataGrid Grid.Row="1" Height="400" AutoGenerateColumns="False" CanUserDeleteRows="False" SelectionUnit="Cell">
<ctrls:DataGrid.ItemsSource>
    <!--<Binding Path="Targets"/>-->
    <MultiBinding Converter="{StaticResource TargetActualListConverter}">
        <Binding Path="Targets"/>
        <Binding Path="Actuals"/>
    </MultiBinding>
</ctrls:DataGrid.ItemsSource>
<ctrls:DataGrid.Columns>
    <ctrls:DataGridTextColumn Header="Date" Binding="{Binding Date,StringFormat={}{0:ddd, MMM d}}"/>
    <!--<ctrls:DataGridTextColumn Header="Target" Binding="{Binding Value}"/>-->
    <ctrls:DataGridTextColumn Header="Target" Binding="{Binding Value[0]}"/>
    <ctrls:DataGridTextColumn Header="Actual" Binding="{Binding Value[1]}"/>
</ctrls:DataGrid.Columns>

そして、ここに私の IMultiValueConverter があります:

class TargetActualListConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        TimeSeries<double> Targets = values[0] as TimeSeries<double>;
        TimeSeries<double> Actuals = values[1] as TimeSeries<double>;
        DateTime[] range = TimeSeries<double>.GetDateRange(Targets, Actuals);//Get min and max Dates
        int count = (range[1] - range[0]).Days;//total number of days
        DateTime currDate = new DateTime();
        TimeSeries<double?[]> combined = new TimeSeries<double?[]>();
        for (int i = 0; i < count; i++)
        {
            currDate = range[0].AddDays(i);
            double?[] vals = { Targets.Dates.Contains(currDate) ? (double?)Targets.GetValueByDate(currDate) : null, Actuals.Dates.Contains(currDate) ? (double?)Actuals.GetValueByDate(currDate) : null };
            combined.Add(new TimeValue<double?[]>(currDate, vals));
        }
        return combined;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        TimeSeries<double?[]> combined = value as TimeSeries<double?[]>;
        TimeSeries<double> Targets = new TimeSeries<double>();
        TimeSeries<double> Actuals = new TimeSeries<double>();

        foreach (TimeValue<double?[]> tv in combined)
        {
            if(tv.Value[0]!=null)
                Targets.Add(new TimeValue<double>(tv.Date,(double)tv.Value[0]));
            if (tv.Value[1] != null)
                Actuals.Add(new TimeValue<double>(tv.Date, (double)tv.Value[1]));
        }
        TimeSeries<double>[] result = { Targets, Actuals };
        return result;

    }
}

値が表示されるので、あまり遠く離れることはできません。

私は何を間違っていますか?または、代わりに、これを行う簡単な方法はありますか?

皆さんありがとう!

4

1 に答える 1

2

コンバーターが原因のようです。ObservableCollection は INotifyCollectionChanged を実装します。これは、コレクションに変更 (追加/削除/置換/移動/リセット) があったときに UI に通知します。これらはすべて、コレクションの内容ではなく、コレクションに対する変更であるため、以前に表示された更新は、クラスが INotifyPropertyChanged を実装したことが原因でした。MultiCoverter は新しいオブジェクトの新しいコレクションを返すため、元のオブジェクトに通知するバインディングがないため、最初のコレクションのデータはこれらに伝播されません。

最初にCompositeCollection Element を見て、それがニーズに合っているかどうかを確認することをお勧めします。

ItemsSource をそのまま設定する代わりに、次のような元のオブジェクトを維持できます。

<ctrls:DataGrid.ItemsSource>
    <CompositeCollection>
            <CollectionContainer Collection="{Binding Targets}" />
            <CollectionContainer Collection="{Binding Actuals}" />
       </CompositeCollection>
</ctrls:DataGrid.ItemsSource>

(「基になるデータの変更に応答しない」とは、コレクションを変更するのではなく、値を変更することを指していると思います。間違っている場合はお知らせください。詳しく調べます。)

編集の追加
これがうまくいかない場合の代替手段は、Target コレクションと Actual コレクションの両方をラップする新しいクラスを作成することです。次に、これらのラッパーを使用して単一の ObservableCollection を作成できます。これは実際には、ValueConverter や CompositeCollection を使用するよりも優れた方法です。どちらを使用しても、元々存在していた機能の一部が失われます。値コンバーターを使用してコレクションを再作成すると、元のオブジェクトに直接バインドされなくなり、プロパティ通知が失われる可能性があります。CompositeCollection を使用すると、操作対象のコレクションを認識する必要があるため、追加/削除/移動などで反復または変更できる単一のコレクションがなくなります。

このタイプのラッピング機能は、WPF で非常に役立ちます。これは、MV-VM デザイン パターンの一部である ViewModel の非常に単純化されたバージョンです。基になるクラスにアクセスして INotifyPropertyChanged または IDataErrorInfo を追加できない場合に使用でき、基になるモデルに状態や相互作用などの追加機能を追加するのにも役立ちます。

これは、両方の初期クラスが同じ Name プロパティを持ち、それらの間で共有されていない INotifyPropertyChanged を実装していない、この機能を示す短い例です。

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        Foo foo1 = new Foo { ID = 1, Name = "Foo1" };
        Foo foo3 = new Foo { ID = 3, Name = "Foo3" };
        Foo foo5 = new Foo { ID = 5, Name = "Foo5" };
        Bar bar1 = new Bar { ID = 1, Name = "Bar1" };
        Bar bar2 = new Bar { ID = 2, Name = "Bar2" };
        Bar bar4 = new Bar { ID = 4, Name = "Bar4" };

        ObservableCollection<FooBarViewModel> fooBar = new ObservableCollection<FooBarViewModel>();
        fooBar.Add(new FooBarViewModel(foo1, bar1));
        fooBar.Add(new FooBarViewModel(bar2));
        fooBar.Add(new FooBarViewModel(foo3));
        fooBar.Add(new FooBarViewModel(bar4));
        fooBar.Add(new FooBarViewModel(foo5));

        this.DataContext = fooBar;
    }
}

public class Foo
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public class Bar
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public class FooBarViewModel : INotifyPropertyChanged
{
    public Foo WrappedFoo { get; private set; }
    public Bar WrappedBar { get; private set; }

    public int ID
    {
        get
        {
            if (WrappedFoo != null)
            { return WrappedFoo.ID; }
            else if (WrappedBar != null)
            { return WrappedBar.ID; }
            else
            { return -1; }
        }
        set
        {
            if (WrappedFoo != null)
            { WrappedFoo.ID = value; }
            if (WrappedBar != null)
            { WrappedBar.ID = value; }

            this.NotifyPropertyChanged("ID");
        }
    }

    public string BarName
    {
        get
        {
            return WrappedBar.Name;
        }
        set
        {
            WrappedBar.Name = value;
            this.NotifyPropertyChanged("BarName");
        }
    }

    public string FooName
    {
        get
        {
            return WrappedFoo.Name;
        }
        set
        {
            WrappedFoo.Name = value;
            this.NotifyPropertyChanged("FooName");
        }
    }

    public FooBarViewModel(Foo foo)
        : this(foo, null) { }
    public FooBarViewModel(Bar bar)
        : this(null, bar) { }
    public FooBarViewModel(Foo foo, Bar bar)
    {
        WrappedFoo = foo;
        WrappedBar = bar;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

そして、ウィンドウで:

 <ListView ItemsSource="{Binding}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}"/>
            <GridViewColumn Header="Foo Name" DisplayMemberBinding="{Binding FooName}"/>
            <GridViewColumn Header="Bar Name" DisplayMemberBinding="{Binding BarName}"/>
        </GridView>
    </ListView.View>
</ListView>
于 2009-05-19T00:20:57.020 に答える