私はこのようなリストボックスに結合することができますWPFでは、ViewModelにバインドして、さまざまなXAML要素をViewModelのメソッドにバインドできますか?
XAML:背後に
<UserControl x:Class="TestDynamicForm123.Views.ViewCustomers"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Margin="10">
<ListBox ItemsSource="{Binding}"/>
</StackPanel>
</UserControl>
コード:
using System.Windows.Controls;
using TestDynamicForm123.ItemTypes;
namespace TestDynamicForm123.Views
{
public partial class ViewCustomers : UserControl
{
public ViewCustomers()
{
InitializeComponent();
DataContext = Customers.GetAll();
}
}
}
ビューモデル:
using System.Collections.ObjectModel;
namespace TestDynamicForm123.ItemTypes
{
class Customers : ItemTypes
{
protected ObservableCollection<Customer> collection;
public static ObservableCollection<Customer> GetAll()
{
ObservableCollection<Customer> customers = new ObservableCollection<Customer>();
customers.Add(new Customer() { FirstName = "Jay", LastName = "Anders", Age = 34 });
customers.Add(new Customer() { FirstName = "Jim", LastName = "Smith", Age = 23 });
customers.Add(new Customer() { FirstName = "John", LastName = "Jones", Age = 22 });
customers.Add(new Customer() { FirstName = "Sue", LastName = "Anders", Age = 21 });
customers.Add(new Customer() { FirstName = "Chet", LastName = "Rogers", Age = 35 });
customers.Add(new Customer() { FirstName = "James", LastName = "Anders", Age = 37 });
return customers;
}
}
}
を
しかし、どのように私は "レベルを上に移動する"ことができますのコードビハインドで顧客自身にバインドし、私のXAML内で顧客にバインド様々な方法、 ListBoxのGetAll()、別のコントロールのGetBest()など
私は背後にあるコードでこの構文を使用してみました:XAMLで
DataContext = new Customers();
そして、これらの2つの構文が、どちらも仕事:
<ListBox ItemsSource="{Binding GetAll}"/> (returns blank ListBox)
<ListBox ItemsSource="{Binding Source={StaticResource GetAll}}"/> (returns error)
私はほかに、それはそう、私はそれなしでいくつかのパターンを下に取得したいのですがSilverlightでもないようだ、たObjectDataProviderを避けるために何かあったことを考え出したので、私はそれを取り出し、私に私のVを拘束VM、データを公開し、バインドしましたが、表示されません:http://stackoverflow.com/questions/802162/why-is-this-view-not-correctly-binding-to-this-viewmodel –