1

ここで何が間違っているのかわかりません。とListBoxが設定されていますが、アプリを実行するとDataContextItemsSourceは何もありません。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

4

2 に答える 2

7

メソッドにバインドすることはできません。それをプロパティにすると、機能するはずです。

リストを DataContext として設定するか、リストを保持する ViewModel を作成することをお勧めします。そうすることで、リストボックスがバインドされるインスタンスをより詳細に制御できます。

お役に立てれば!

于 2010-08-17T14:22:26.383 に答える
1

プロパティを使用する必要があるときに、バインディングでメソッドを呼び出そうとしています。それをプロパティに変更すると、準備が整います。

public List<Template> MyTemplates {get; private set;}

public TemplateList()
{
    InitializeComponent();
    SetTemplates();
    DataContext = this;
}

// ItemsSource of ListBox
public void SetTemplates()
{
    // do stuff to set up the MyTemplates proeprty
    MyTemplates = something.ToList();
}

Xaml:

<ListBox ItemsSource="{Binding Path=MyTemplates}" Grid.Row="1" Grid.Column="1"
   Width="400" Height="300" DisplayMemberPath="Name"
   SelectedValuePath="Name"/>
于 2010-08-17T14:22:50.103 に答える