ここで何が間違っているのかわかりません。とListBox
が設定されていますが、アプリを実行するとDataContext
にItemsSource
は何もありません。ListBox
デバッグ時に、アイテムを取得するためのメソッドの最初の行ListBox
がヒットすることはありません。ここに私が持っているものがあります:
// Constructor in UserControl
public TemplateList()
{
_templates = new Templates();
InitializeComponent();
DataContext = this;
}
// ItemsSource of ListBox
public List<Template> GetTemplates()
{
if (!tryReadTemplatesIfNecessary(ref _templates))
{
return new List<Template>
{
// Template with Name property set:
new Template("No saved templates", null)
};
}
return _templates.ToList();
}
ここに私のXAMLがあります:
<ListBox ItemsSource="{Binding Path=GetTemplates}" Grid.Row="1" Grid.Column="1"
Width="400" Height="300" DisplayMemberPath="Name"
SelectedValuePath="Name"/>
Template
クラスのインスタンスには、Name
単なるstring
. 私が望むのは、テンプレート名のリストを表示することだけです。ユーザーは のデータを変更しません。読み取り専用にTemplate
するListBox
必要があります。
Template には、Data
後で this に表示するプロパティもあります。そのため、文字列のリストだけListBox
を返すようにしたくありません。オブジェクトGetTemplates
のコレクションを返す必要があります。Template