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

时间:2017-07-28 13:43:58

标签: c# wpf xaml mvvm

  

关于:   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));
            }
        }
    } }

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

    } }

View / UserControl 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>

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

1 个答案:

答案 0 :(得分:0)

设置“类别”和“位置”属性时,应该两次引发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");
            }
        }
    }

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