使用MVVM

时间:2018-05-05 11:19:39

标签: wpf text mvvm model textbox

如何在Model层中存储TextBox的内容以符合MVVM?

我已经制作了简单的演示应用程序来练习MVVM。它由主TextBox和另外两个TextBox组成,仅用于测试应用程序是否正常工作。

在ViewModel中我有TextContent类,它实现了INotifyPropertyChanged,它有Text属性,MainTextBox的Text绑定到它,它可以正常工作。

在Model I中有TextStore属性,我尝试使用简单方法ModelUpdate()在ViewModel.TextContent的Text属性的setter中更新。

此模型更新无效。

您能否告诉我,我可以将存储在ViewModel属性中的TextBox内容传输到Model层吗?并且符合MVVM模式?

这里是代码:

查看:(这里,第三个TextBox绑定到模型 - 我知道,这与MVVM的想法不兼容,但这只是用于检查Model层中TextStore属性的值)

 <Window x:Class="MVVM_TBDB_2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:MVVM_TBDB_2"
            xmlns:vm="clr-namespace:MVVM_TBDB_2.ViewModel"
            xmlns:m="clr-namespace:MVVM_TBDB_2.Model"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <m:TextContent x:Key="ModelTextContent" />
        </Window.Resources>
        <Window.DataContext>
            <vm:TextContent />
        </Window.DataContext>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="8*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <TextBox Name="MainTB" Grid.Row="0" Margin="10" AcceptsReturn="True" 
                    Text="{Binding Text, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"/>
            <Button Name="SaveButton" Content="Save" Grid.Row="1" Margin="10,2" Padding="20,0" HorizontalAlignment="Left"   />
            <TextBox Name="ControlTB" Grid.Row="1" Margin="30,2,2,2" Width="100" Text="{Binding Text, Mode=OneWay}" />
            <TextBox Name="ControlTB2" Grid.Row="1" Margin="300,2,2,2" Width="100" DataContext="{StaticResource ModelTextContent}"
                    Text="{Binding TextStock, Mode=TwoWay}" />
        </Grid>
    </Window>

视图模型:

    class TextContent : INotifyPropertyChanged
        {
            private Model.TextContent model;

            public TextContent()
            {
                model = new Model.TextContent();
            }

            private string _Text;

            public string Text
            {
                get { return _Text; }
                set
                {

                    _Text = value;

                    OnPropertyChanged("Text");
                    ModelUpdate(_Text);

                }
            }

            private void OnPropertyChanged(string parameter)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(parameter));
            }

            public event PropertyChangedEventHandler PropertyChanged;

            private void ModelUpdate(string textToUpdate)
            {
                model.TextStock = textToUpdate;
            }
        }

型号:

  class TextContent
  {
      private string _TextStock;

      public string TextStock
      {
          get { return _TextStock; }
          set { _TextStock = value; }
      }

  }

1 个答案:

答案 0 :(得分:0)

请参阅此处我已实施您的要求。

  1. 从代码后面附加数据上下文。
  2. 在Model。中实现INotifyPropertyChanged接口。
  3. 将TextStock属性设为binded属性。
  4. MainWindow.cs

     public TextContent _model { get; set; }
    
        public TextContentViewModel _viewModel { get; set; }
        public MainWindow()
        {
            InitializeComponent();
    
            _viewModel = new TextContentViewModel();
            _model = new TextContent();
    
            this.DataContext = _viewModel;
            ControlTB2.DataContext = _model;
        }
    

    您的ViewModel类

    private TextContent model;
    
        public TextContentViewModel()
        {
    
        }
    
        private string _Text;
    
        public string Text
        {
            get { return _Text; }
            set
            {
    
                _Text = value;
    
                OnPropertyChanged("Text");
                if (model != null)
                {
                    ModelUpdate(_Text);
                }
                else
                {
                    model = ((Application.Current.MainWindow as MainWindow).ControlTB2).DataContext as TextContent;
                }
    
            }
        }
    
        private void OnPropertyChanged(string parameter)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(parameter));
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void ModelUpdate(string textToUpdate)
        {
            model.TextStock = textToUpdate;
        }
    }
    

    模型类

    私人字符串_TextStock;

        public string TextStock
        {
            get { return _TextStock; }
            set { _TextStock = value; OnPropertyChanged("TextStock"); }
        }
    
        private void OnPropertyChanged(string parameter)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(parameter));
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    

    注意:我已按照自己的方便重命名了类名。