簡體   English   中英

使用MVVM在模型層中的WPF TextBox內容

[英]WPF TextBox content in Model layer using MVVM

如何將TextBox的內容存儲在Model層中以符合MVVM?

我已經制作了簡單的演示應用程序來練習MVVM。 它由主要TextBox和2個其他TextBox組成,僅用於測試應用程序是否正常運行。

在ViewModel中,我具有TextContent類,該類實現了InotifyPropertyChanged,並且具有Text屬性,MainTextBox的Text綁定到此上,並且可以正常工作。

在模型中,我具有TextStore屬性,我嘗試使用簡單的方法ModelUpdate()在ViewModel.TextContent的Text屬性的設置器中進行更新。

而且此模型更新無效。

您能告訴我如何將存儲在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>

ViewModel:

    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. 從后面的代碼附加數據上下文。
  2. 在模型中實現INotifyPropertyChanged接口。
  3. 使TextStock屬性為綁定屬性。

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;

注意:為方便起見,我已將類名重命名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM