まだ説明がない興味深い問題に出くわしました...
以下の非常に単純なMVVMWPFアプリケーションを考えると、ViewModelでの表示がに設定されている場合にのみ、リストがコンボボックスにバインドされるのはなぜpublic
ですか?
TestList
可視性をに変更するinternal
と、コンパイル時にエラーや警告は発生しませんが、実行時にはコンボボックスは空のままになります。
公式ドキュメントの引用:internal
タイプまたはメンバーは、同じアセンブリ内のファイル内でのみアクセスできます。
この問題は、ViewとViewModelが同じアセンブリで定義されているにもかかわらず発生しています。
コードは次のようになります。
モデル:
class TestModel
{
internal List<string> Musketeers { get; private set; }
public TestModel()
{
Musketeers = new List<string> { "Athos", "Porthos", "Aramis" };
}
}
意見:
<Window x:Class="TestWpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<ComboBox Width="250" Height="25" ItemsSource="{Binding TestList}" />
</Grid>
</Window>
ViewModel:
class TestViewModel : INotifyPropertyChanged
{
TestModel myModel = new TestModel();
public List<string> TestList
{
get
{
return myModel.Musketeers;
}
}
// INotifyPropertyChanged members are below ...
}