0

LINQクエリを入力している非常に単純なクラスがあります。すべてうまく機能します。

public class CatSummary : INotifyPropertyChanged
{
    private string _catName;
    public string CatName
    {
        get { return _catName; }
        set { if (_catName != value) { _catName = value; NotifyPropertyChanged("CatName"); } }
    }

    private decimal _catAmount;
    public decimal CatAmount
    {
        get { return _catAmount; }
        set { if (_catAmount != value) { _catAmount = value; NotifyPropertyChanged("CatAmount"); } }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    // Used to notify Silverlight that a property has changed.
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

            //MessageBox.Show("NotifyPropertyChanged: " + propertyName);

        }
    }

}

LINQビット;

        var myOC = new ObservableCollection<CatSummary>();


        var initialQuery = BoughtItemDB.BoughtItems
                         .GroupBy(item => item.ItemCategory)
                         .Select(x => new CatSummary
                          { 
                              CatName = x.Key, 
                              CatAmount = x.Sum(amt => amt.ItemAmount)
                          });

        foreach (var item in initialQuery) myOC.Add(item);

以下のXAMLでWPFコントロールをカスタムクラスにバインドしようとしています。

<ListBox x:Name="boughtItemsListBox" ItemsSource="{Binding CatSummary}" Margin="5,27,-35,100" Width="450" Height="371" Grid.ColumnSpan="2" Grid.RowSpan="2">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid HorizontalAlignment="Stretch" Width="440">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="150" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Column="0" Text="{Binding CatName, StringFormat=g}" TextWrapping="Wrap"  FontSize="{StaticResource PhoneFontSizeSmall}" VerticalAlignment="Top"/>
                <TextBlock Grid.Column="1" Text="{Binding CatAmount, StringFormat=\{0:C\}}" Margin="1" FontSize="{StaticResource PhoneFontSizeSmall}" VerticalAlignment="Top"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

これにより、次のエラーが発生します。BindingExpressionパスエラー:「MyApp.SpendAnalysis+CatSummary」に「CatSummary」プロパティが見つかりません

私が読んだことによると、クラスのプロパティをObservableCollectionプロパティにする必要があると思いますが、これは私のLINQクエリを壊すようです。私はこれについてあらゆる種類のことを試しましたが、これがどのように機能するかを理解するのに役立つチュートリアルを見つけることもできません。どんなアドバイスやポインタも喜んで受け取りました。

4

2 に答える 2

2

XAMLバインディングでは、DataContextofは、typeboughtItemsListBoxという名前のプロパティを持つアイテムである必要があります(または、リストを構成するアイテムをリアルタイムで変更できるようにする場合は、、またはの他の実装者)。私はこれがあなたが間違っているところだと思います。CatSummaryIEnumerable<CatSummary>ObservableCollection<CatSummary>INotifyCollectionChanged

たとえば、実際にのようなものがある場合boughtItemsListBox.DataContext = myOC;、それは何も探してmyOC.CatSummaryいません。実行する必要があるのは、XAMLをにItemsSource="{Binding}"変更するか、コードをに変更することですboughtItemsListBox.ItemsSource = myOC;(そして、XAMLの現在は役に立たないItemsSource設定を削除します)。

于 2012-04-17T22:36:17.513 に答える
1

MyOCプロパティがあると仮定します

private ObservableCollection<CatSummary> _myOC = new ObservableCollection<CatSummary>();
public ObservableCollection<CatSummary> MyOC
{
    get { return _myOC ; }
    set { if (_myOC != value) { _myOC = value; NotifyPropertyChanged("MyOC"); } }
}

にバインドするListBoxと、ObservableCollectionそれぞれListBoxItemDataContextタイプのになりCatSummaryます。

<ListBox x:Name="boughtItemsListBox" ItemsSource="{Binding MyOc}" ...

LINQクエリコードで

var initialQuery = BoughtItemDB.BoughtItems
                    .GroupBy(item => item.ItemCategory)
                    .Select(x => new CatSummary
                     { 
                         CatName = x.Key, 
                         CatAmount = x.Sum(amt => amt.ItemAmount)
                     });

MyOCを再割り当て

MyOC = new ObservableCollection<CatSummary>(initialQuery.ToList());

または、既存のMyOCコレクションを使用します。

MyOC.Clear();
foreach (var item in initialQuery) MyOC.Add(item);
于 2012-04-18T08:51:03.420 に答える