MVVM - 遵循教程,但未按预期工作

  • 本文关键字:工作 教程 MVVM c# wpf xaml mvvm
  • 更新时间 :
  • 英文 :


about : https://www.tutorialspoint.com/mvvm/mvvm_wpf_data_bindings.htm

MVVM 相当新,所以我正在按照教程来了解有关它的更多信息。

但是,我已经到达了教程(上面的链接(中似乎不起作用的点。 基本上,当我更新文本框时,文本块也应该通过数据绑定进行更新

这是我的代码。

型号 : https://pastebin.com/Lu2Cw2Py

using System.ComponentModel;
namespace MIDB_MVVM.Model {
public class InventoryModel
{
}
public class InventoryItems : INotifyPropertyChanged
{
#region Variables
private string _barcode;
private string _category;
private string _location;
private string _status;
private string _sizeCase;
private string _name;
private string _manufacturer;
private string _mpn;
private string _supplier;
private string _spn;
private string _value;
private string _tolerance;
private string _voltage;
private string _powerRating;
private string _rohs;
private string _description;
#endregion
public string Barcode
{
get
{
return _barcode;
}
set
{
if (_barcode != value)
{
_barcode = value;
RaisePropertyChanged("Barcode");
}
}
}
public string Category
{
get
{
return _category;
}
set
{
if (_category != value)
{
_category = value;
RaisePropertyChanged("Category");
}
}
}
public string Location
{
get
{
return _location;
}
set
{
if (_location != value)
{
_location = value;
RaisePropertyChanged("Location");
}
}
}
public string Status
{
get
{
return _status;   
}
set
{
if (_status != value)
{
_status = value;
RaisePropertyChanged("Status");
}
}
}
public string Sizecase
{
get
{
return _sizeCase;
}
set
{
if (_sizeCase != value)
{
_sizeCase = value;
RaisePropertyChanged("Sizecase");
}
}
}
public string Name
{
get
{
return _name;
}
set
{
if (_name != value)
{
_name = value;
RaisePropertyChanged("Name");
}
}
}
public string Manufacturer
{
get
{
return _manufacturer;
}
set
{
if (_manufacturer != value)
{
_manufacturer = value;
RaisePropertyChanged("Manufacturer");
}
}
}
public string Mpn
{
get
{
return _mpn;
}
set
{
if (_mpn != value)
{
_mpn = value;
RaisePropertyChanged("Mpn");
}
}
}
public string Supplier
{
get
{
return _supplier;
}
set
{
if (_supplier != value)
{
_supplier = value;
RaisePropertyChanged("Supplier");
}
}
}
public string Spn
{
get
{
return _spn;
}
set
{
if (_spn != value)
{
_spn = value;
RaisePropertyChanged("spn");
}
}
}
public string Value
{
get
{
return _value;
}
set
{
if (_value != value)
{
_value = value;
RaisePropertyChanged("Value");
}
}
}
public string Tolerance
{
get
{
return _tolerance;
}
set
{
if (_tolerance != value)
{
_tolerance = value;
RaisePropertyChanged("Tolerance");
}
}
}
public string Voltage
{
get
{
return _voltage;
}
set
{
if (_voltage != value)
{
_voltage = value;
RaisePropertyChanged("Voltage");
}
}
}
public string PowerRating
{
get
{
return _powerRating;
}
set
{
if (_powerRating != value)
{
_powerRating = value;
RaisePropertyChanged("PowerRating");
}
}
}
public string Rohs
{
get
{
return _rohs;
}
set
{
if (_rohs != value)
{
_rohs = value;
RaisePropertyChanged("Rohs");
}
}
}
public string Description
{
get
{
return _description;
}
set
{
if (_description != value)
{
_description = value;
RaisePropertyChanged("Description");
}
}
}
public string CategoryLocation
{
get { return _category + " " + _location; }
}
private string conString = Properties.Settings.Default.InventoryDBConnString;
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
} }

视图型号: https://pastebin.com/fHtFVyx8

using System.Collections.ObjectModel;
using MIDB_MVVM.Model;
namespace MIDB_MVVM.ViewModel {
public class InventoryViewModel
{
public InventoryViewModel()
{
LoadInventory();
}
public ObservableCollection<InventoryItems> Inventories
{
get;
set;
}
public void LoadInventory()
{
ObservableCollection<InventoryItems> inventories = new ObservableCollection<InventoryItems>();
inventories.Add(new InventoryItems
{
Barcode = "MRES01",
Category = "Resistor",
Description = "MRES01",
Location = "ResBox", 
Manufacturer = "RS",
Mpn = "MRES01",
Name = "SMDRes",
PowerRating = "10",
Rohs = "Yes",
Sizecase = "0402",
Value = "10",
Status = "New",
Supplier = "Farnell",
Tolerance = "10", 
Voltage = "10",
Spn = "01234589"
});
inventories.Add(new InventoryItems
{
Barcode = "MRES02",
Category = "Resistor",
Description = "MRES02",
Location = "ResBox",
Manufacturer = "RS",
Mpn = "MRES01",
Name = "SMDRes",
PowerRating = "20",
Rohs = "Yes",
Sizecase = "0603",
Value = "20",
Status = "New",
Supplier = "Farnell",
Tolerance = "20",
Voltage = "20",
Spn = "23456789"
});
inventories.Add(new InventoryItems
{
Barcode = "MRES03",
Category = "Resistor",
Description = "MRES03",
Location = "ResBox",
Manufacturer = "RS",
Mpn = "MRES01",
Name = "SMDRes",
PowerRating = "30",
Rohs = "Yes",
Sizecase = "0805",
Value = "30",
Status = "New",
Supplier = "Farnell",
Tolerance = "30",
Voltage = "30",
Spn = "987654321"
});
Inventories = inventories;
}
} }

视图/用户控件 Xaml:https://pastebin.com/4bLFtss0

<UserControl x:Class="MIDB_MVVM.Views.InventoryView"
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:MIDB_MVVM.Views"
xmlns:viewModel = "clr-namespace:MIDB_MVVM.ViewModel" 
xmlns:vml ="clr-namespace:MIDB_MVVM.VML"
vml:ViewModelLocator.AutoHookedUpViewModel="True"
mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300">
<!--<UserControl.DataContext>
<viewModel:InventoryViewModel/>
</UserControl.DataContext>-->
<Grid>
<StackPanel HorizontalAlignment = "Left">
<ItemsControl ItemsSource = "{Binding Path = Inventories}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation = "Horizontal">
<TextBox Text = "{Binding Path = Category, Mode = TwoWay}" 
Width = "100" Margin = "3 5 3 5"/>
<TextBox Text = "{Binding Path = Location, Mode = TwoWay }" 
Width = "100" Margin = "0 5 3 5"/>
<TextBlock Text = "{Binding Path = CategoryLocation, Mode = OneWay}" 
Margin = "0 5 3 5"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Grid> </UserControl>

有人能告诉我我在这里做错了什么吗?基本上,我试图弄清楚为什么当我的文本框中的值更改时我的文本块没有更新?

设置"类别"和"位置"属性时,应引发 PropertyChanged 事件两次,一次使用属性名称,另一个通知 CategoryLocation 也已更改:

public string Category
{
get
{
return _category;
}
set
{
if (_category != value)
{
_category = value;
RaisePropertyChanged("Category");
RaisePropertyChanged("CategoryLocation");
}
}
}
public string Location
{
get
{
return _location;
}
set
{
if (_location != value)
{
_location = value;
RaisePropertyChanged("Location");
RaisePropertyChanged("CategoryLocation");
}
}
}

否则,视图无法知道更改两个属性中的一个会影响第三个属性。

最新更新