【问题标题】:MVVM - Following a tutorial but not working as intended [closed]MVVM - 遵循教程但未按预期工作[关闭]
【发布时间】:2017-07-28 13:43:57
【问题描述】:

关于: 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>

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

【问题讨论】:

  • “不工作”不能作为问题描述。请更具体。您期望它做什么,为什么期望它这样做,以及您期望它做什么?
  • 如文章底部所述,“基本上,我想弄清楚为什么当我的文本框中的值发生更改时我的文本块没有更新?” - 关于用户控制或视图
  • 另外,all 的代码对于理解问题实际上是必要的吗?请阅读如何创建minimal reproducible example。

标签: c# wpf xaml mvvm


【解决方案1】:

当您设置 Category 和 Location 属性时,您应该引发 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");
            }
        }
    }

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

【讨论】:

  • 根本看不到这个,需要做更多关于这个的阅读。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2019-03-01
  • 1970-01-01
  • 2016-11-19
  • 1970-01-01
  • 1970-01-01
  • 2013-05-13
  • 2018-04-27
  • 2018-03-21
相关资源
最近更新 更多