나는이 같은리스트 박스에 바인딩 할 수 있습니다 :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 내에서 다양한 방법으로 바인딩합니다., 예 : GetAll() ListBox 및 GetBest() 다른 컨트롤, 등?
내가 뒤에있는 코드에서이 구문을 시도 :
DataContext = new Customers();
그리고 XAML이 두 구문 그러나 둘 다 일을 :
당신은 방법에 바인딩ObjectDataProvider
를 사용할 필요가
<ListBox ItemsSource="{Binding GetAll}"/> (returns blank ListBox)
<ListBox ItemsSource="{Binding Source={StaticResource GetAll}}"/> (returns error)
나는 ObjectDataProvider 내가 없이는까지의 패턴을 좀하고 싶습니다 실버 라이트에 없을 것뿐만 아니라, 피하기 위해 어떤 것을 생각, 그래서 그것을 꺼내, 내 V를 바인딩 내 VM, 노출 된 데이터, 바인딩 된 있지만 나타나지 않는 : http://stackoverflow.com/questions/802162/why-is-this-view-not-correctly-binding-to-this-viewmodel –