实施您的
Coffee.cs
具有
INotifyPropertyChanged
以便相应地与如下视图进行通信:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Newtonsoft.Json;
namespace MVVM.Model
{
public class Coffee : INotifyPropertyChanged
{
private string _type;
private double _price;
private string _roast;
private string _origin;
[JsonProperty("coffee_type")]
public string Type
{
get => _type;
set
{
if (value != _type)
{
_type = value;
RaisePropertyChanged(nameof(Type));
}
}
}
[JsonProperty("price")]
public double Price
{
get => _price;
set
{
if (value != _price)
{
_price = value;
RaisePropertyChanged(nameof(Price));
}
}
}
[JsonProperty("roast_level")]
public string Roast
{
get => _roast;
set
{
if (value != _roast)
{
_roast = value;
RaisePropertyChanged(nameof(Roast));
}
}
}
[JsonProperty("origin_country")]
public string Origin
{
get => _origin;
set
{
if (value != _origin)
{
_origin = value;
RaisePropertyChanged(nameof(Origin));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
更改您的
CoffeeViewModel.cs
到
LoadCoffee
如下所示:
using System.Collections.ObjectModel;
using System.ComponentModel;
using Newtonsoft.Json;
using MVVM.Model;
namespace MVVM.ViewModel
{
public class CoffeeViewModel : INotifyPropertyChanged
{
private ObservableCollection<Coffee> _coffeeList;
public ObservableCollection<Coffee> CoffeeList
{
get => _coffeeList;
set
{
if (value != _coffeeList)
{
_coffeeList = value;
RaisePropertyChanged(nameof(CoffeeList));
}
}
}
public CoffeeViewModel()
{
LoadCoffee();
}
public void LoadCoffee()
{
string json = @"
[
{ 'coffee_type': 'Espresso', 'price': 2.5, 'roast_level': 'Dark', 'origin_country': 'Italy' },
{ 'coffee_type': 'Latte', 'price': 3.0, 'roast_level': 'Medium', 'origin_country': 'France' }
]";
var coffeeModels = JsonConvert.DeserializeObject<List<Coffee>>(json);
CoffeeList = new ObservableCollection<Coffee>(coffeeModels);
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
你的观点是这样的:
<UserControl x:Class="MVVM.Views.CoffeeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MVVM.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<StackPanel HorizontalAlignment="Left">
<ItemsControl ItemsSource="{Binding Path=CoffeeList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Path=Type, Mode=TwoWay}" Width="100" Margin="3 5 3 5"/>
<TextBox Text="{Binding Path=Price, Mode=TwoWay}" Width="100" Margin="0 5 3 5"/>
<TextBlock Text="{Binding Path=Roast, Mode=OneWay}" Margin="0 5 3 5"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Grid>
</UserControl>
最后别忘了设置
DataContext
属于
MainWindow
到
CoffeeViewModel
如下所示:
<Window x:Class="MVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<Grid>
<local:CoffeeView />
</Grid>
</Window>
通过以上操作,您将能够将json中的数据绑定到viewModel