次の静的クラスがあります
static class ContactSettings
{
static ObservableCollection<Contact> _contactCollection = new ObservableCollection<Contact>();
public static ObservableCollection<Contact> ContactCollection
{
get { return _contactCollection; }
}
}
ここで、Contact は、Contact.Name および Contact.Address 文字列プロパティを持つクラスです。
上記の ContactCollection を Window に存在する WPF ListView にバインドしたいと考えています。
これが私の ListView XAML 定義です
<ListView x:Name="_contactListView" DataContext="{Binding Path=ContactSettings}" ItemsSource="{Binding ContactSettings.ContactCollection}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Address" DisplayMemberBinding="{Binding Address}" />
</GridView>
</ListView.View>
</ListView>
バインディングが機能しません。XAML 内の DataContext および ItemSource プロパティに問題があると確信しています。ContactCollection を Window クラス内に移動し、DataContext を Self に設定すると、コードが機能するようになります。問題は、ListView に別のクラス内のコレクションにバインドするように指示する方法がわからないことです。ご協力いただきありがとうございます。