このViewがこのViewModelに正しくバインドされないのはなぜですか?
-
03-07-2019 - |
質問
DataObjectProviderなしでビューモデルにビューをバインドしようとしています。
次のコードはエラーなしで実行されますが、リストボックスは空です。
私が知る限り、私は正しく:
- ビューのDataContextをViewModel自体に設定します(
DataContext = new CustomersViewModel();
) - ViewModel内の顧客オブジェクトを公開します(
public static ObservableCollection<Customer> GetAll()
) - ListBoxを顧客オブジェクトにバインドします(
ItemsSource="{Binding GetAll}"
)
ここで不足している構文の小さな部分
CustomersView.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>
CustomersView.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()
の実装をコンストラクターに移動してから、コレクションをクラスのインスタンスプロパティとして公開する必要があります。
所属していません StackOverflow