문제

DataObjectProvider없이 내보기 모델에 내보기를 바인딩하려고합니다.

그만큼 다음은 오류없이 실행됩니다,하지만 내 목록 상자입니다 비어 있는.

내가 알 수있는 한, 나는 정확하게 :

  • View의 데이터 콘텍스트를 ViewModel 자체로 설정합니다 (DataContext = new CustomersViewModel();)
  • 뷰 모델에 고객 객체를 노출시킵니다 (public static ObservableCollection<Customer> GetAll())
  • Listbox를 고객 객체에 바인딩합니다 (ItemsSource="{Binding GetAll}")

여기서 내가 누락 된 작은 구문 조각은 무엇입니까?

customerView.xaml :

<UserControl x:Class="TestDynamicForm123.View.CustomersView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <UserControl.Resources>
        <Style x:Key="allCustomersListBox" TargetType="ListBox">
            <Setter Property="BorderThickness" Value="0"/>
        </Style>
        <DataTemplate x:Key="allCustomersDataTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding FirstName}"/>
                <TextBlock Text=" "/>
                <TextBlock Text="{Binding LastName}"/>
            </StackPanel>
        </DataTemplate>
    </UserControl.Resources>

    <StackPanel Style="{StaticResource viewWrappingStackPanel}">
        <ListBox ItemsSource="{Binding GetAll}"
                 ItemTemplate="{StaticResource allCustomersDataTemplate}"
                 Style="{StaticResource allCustomersListBox}">
        </ListBox>
    </StackPanel>
</UserControl>

customerView.xaml.cs :

using System.Windows.Controls;
using TestDynamicForm123.ViewModel;

namespace TestDynamicForm123.View
{
    public partial class CustomersView : UserControl
    {
        public CustomersView()
        {
            InitializeComponent();

            DataContext = new CustomersViewModel();
        }
    }
}

CustomersViewModel.cs :

using System.Collections.ObjectModel;

namespace TestDynamicForm123.ViewModel
{
    public class CustomersViewModel
    {
        public static ObservableCollection<Customer> GetAll()
        {
            return Customer.GetAll();
        }
    }
}

customer.cs : (모델)

using System.Collections.ObjectModel;

namespace TestDynamicForm123.ViewModel
{
    public class Customer
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }

        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;
        }

    }
}
도움이 되었습니까?

해결책

문제는 CustomerViewModel의 getAll이 인스턴스 속성이 아닌 정적 함수 일 때 CustomerViewModel.getall ()에 바인딩하려고한다는 것입니다.

정적 기능을 이와 같은 것으로 변경하십시오 (CustomerViewModel에서) :

public ObservableCollection<Customer> GetAll
{
    get { return Customer.GetAll(); }
}

다른 팁

문제는 그 것입니다 CustomersViewModel .GetAll() 정적 방법입니다. 바인딩은 인스턴스 속성과 만 작동합니다. 구현을 이동해야합니다 GetAll() 생성자에게 컬렉션을 클래스의 인스턴스 속성으로 노출시킵니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top