2

ObservableCollection を ListBox にバインドしようとしています。デバッグからの出力にはバインディング エラーは表示されませんが、何らかの理由で機能しません。

xaml:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:nwsfeed"
x:Class="nwsfeed.MainWindow"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
x:Name="Window">

    <ListBox x:Name="listBoxChannels" 
    ItemsSource="{Binding ElementName=Window, Path=App.ActiveProfile.Feeds}"
    DisplayMemberPath="Title"/>

</Window>

コード:

public partial class MainWindow : Window {
    public NwsfeedApp App { get; set; }
    // ..
}

public sealed class NwsfeedApp {
    public UserProfile ActiveProfile { get; set; }
    //..
}

public class UserProfile {
    private ObservableCollection<RSSFeed> feeds;
    public ObservableCollection<RSSFeed> Feeds { get { return feeds; } }
    //..
}

edit : 問題は、ObservableCollection を MainWindow のパブリック プロパティとして持っていて、それを次のようにバインドすると、機能することです。

ItemsSource="{Binding ElementName=Window, Path=Items, Mode=OneWay}" 

しかし、私がこれを行うと、そうではありません:

ItemsSource="{Binding ElementName=Window, Path=App.ActiveProfile.Feeds, Mode=OneWay}" 

edit2 App、ActiveProfile、および Feeds プロパティに INotifyPropertyChanged を実装しました。ListBox は、.Items.Refresh() を呼び出さない限り、コレクションの変更を反映しません。

助言がありますか?ありがとう。

4

2 に答える 2

1

クラスにINotifyPropertyChangedを実装します。

于 2012-04-18T11:09:43.540 に答える
0

情報が不足しているため、暗闇でのショットを次に示します。

Appウィンドウの分離コード内でインスタンス化された変数ではない静的アプリケーション プロパティにアクセスしようとしています。App が見つからないため、バインディングの残りのプロパティ パスは評価されません。

おそらくこれが必要です:

ItemsSource="{x:Static local:App.ActiveProfile.Feeds}"

欠落している出力についてはTools->Options->Debugging->Output window->WPF Trace Settings、利用可能な WPF トレースに関する情報を多かれ少なかれ表示するオプションがあることを試してください。

HTH、

バブ。

于 2012-04-18T11:18:23.913 に答える