使用MVVM框架中的按钮动态添加文本框

rsgmon

我一直在爬陡峭的WPF山!因此,我想创建一个允许用户动态添加文本框的UI。为此,他们将按一下按钮。

我已经设法在后面使用代码创建了此代码,但我想转向MVVM结构,因此视图中没有任何代码。我已经尝试过ICommand和ObservableCollection,但是我缺少了一些东西,我也不知道在哪里。这是我的简单示例。

XAML:非常基本,只有一个按钮可以添加一行。

<Window x:Class="WPFpractice072514.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WPFpractice072514"
        Title="MainWindow" Height="350" Width="525">
    <Grid Name="mymy" >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Button Grid.Column="0" Grid.Row="0" Name="ButtonUpdateArtist"
                Content="Add TextBox" Click="ButtonAddTexboxBlockExecute" />

    </Grid>
</Window>

背后的C#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFpractice072514
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region members
        int count = 0;
        #endregion

        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonAddTexboxBlockExecute(Object Sender, RoutedEventArgs e)
        {
            TextBox t = new TextBox();
            t.Height = 20;
            t.Width = 20;
            t.Name = "button";

            RowDefinition rowDef1;
            rowDef1 = new RowDefinition();
            mymy.RowDefinitions.Add(rowDef1);

            ColumnDefinition colDef1;
            colDef1 = new ColumnDefinition();
            mymy.ColumnDefinitions.Add(colDef1);
            ++count;

            mymy.Children.Add(t);

            Grid.SetColumn(t, 1);
            Grid.SetRow(t, count);

        }
    }
}

问题:我需要什么代码(XAML和C#)才能将方法从代码后面移到视图模型中?

您可以使用命令动态添加文本框吗?

我假设文本框必须保存在一个容器中,在这种情况下,该容器是用于网格的。但是,如果我使用的是MVVM,是否需要在列表视图或使用ItemsSource的其他容器中包含文本框?

罗希特·瓦茨

请按照以下步骤操作,您已完成:

  1. 使用ItemsControl它并将其绑定ItemsSource到ViewModel中的某个集合(最好是ObservableCollection)。
  2. ItemTemplate为其中包含TextBox的ItemsControl定义
  3. ICommand在ViewModel中创建一个并将其绑定到button。
  4. 在命令上执行集合中的添加项,您将看到TextBox被自动添加。

XAML

<StackPanel>
    <Button Content="Add TextBox" Command="{Binding TestCommand}"/>
    <ItemsControl ItemsSource="{Binding SomeCollection}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Path=.}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</StackPanel>

ViewModel

public class MainWindowViewModel : INotifyPropertyChanged
{
    public ObservableCollection<string> SomeCollection { get; set; }
    public ICommand TestCommand { get; private set; }

    public MainWindowViewModel()
    {
        SomeCollection = new ObservableCollection<string>();
        TestCommand = new RelayCommand<object>(CommandMethod);
    }

    private void CommandMethod(object parameter)
    {
        SomeCollection.Add("Some dummy string");
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

RelayCommand

public class RelayCommand<T> : ICommand
{    
    readonly Action<T> _execute = null;
    readonly Predicate<T> _canExecute = null;

    public RelayCommand(Action<T> execute)
        : this(execute, null)
    {
    }    

    public RelayCommand(Action<T> execute, Predicate<T> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute((T)parameter);
    }    

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
        _execute((T)parameter);
    }
}

-我假设您知道如何通过设置DataContext使绑定魔术起作用来将View与ViewModel插入。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用jquery在文本框中动态添加值

来自分类Dev

使用动态添加的按钮删除动态添加的 TinyMCE 文本框

来自分类Dev

在jQuery中动态添加文本框

来自分类Dev

在WPF中动态添加文本框

来自分类Dev

在HTML中动态添加文本框

来自分类Dev

动态添加时删除标记为按钮的文本框

来自分类Dev

如何使用动态单选按钮添加事件以显示文本框

来自分类Dev

使用实体框架,我想将数据添加到文本框中

来自分类Dev

在jsp中动态添加<form:input>文本框

来自分类Dev

在动态生成的文本框中添加on key up事件

来自分类Dev

在VB.NET中动态添加文本框

来自分类Dev

格式化在Excel中动态添加的文本框

来自分类Dev

在BIRT报告中添加文本框并提交按钮

来自分类Dev

单击Angular JS中的按钮添加新文本框

来自分类Dev

通过单击按钮使用javascript添加文本框

来自分类Dev

使用javascript在按钮单击上添加文本框

来自分类Dev

如何使用C#在gridview中读取动态添加的文本框值?

来自分类Dev

使用Selenium从只读文本框中获取动态文本

来自分类Dev

如何使用实体框架将 WPF 文本框中的新文本值添加到数据库?

来自分类Dev

我正在尝试使用JavaScript在按钮的onclick的有序列表中添加文本框,并在添加文本框时增加ID名称

来自分类Dev

如何删除动态添加的文本框?

来自分类Dev

通过绑定动态添加文本框

来自分类Dev

javascript以动态添加文本框

来自分类Dev

从文本框中动态获取文本

来自分类Dev

使用C#中的按钮清除多个文本框

来自分类Dev

使用按钮数组从文本框数组中获取价值

来自分类Dev

使用按钮更新templateField中的文本框

来自分类Dev

如何使用Java禁用单选按钮中的文本框

来自分类Dev

单击按钮可更改动态生成的焦点文本框中的文本

Related 相关文章

  1. 1

    使用jquery在文本框中动态添加值

  2. 2

    使用动态添加的按钮删除动态添加的 TinyMCE 文本框

  3. 3

    在jQuery中动态添加文本框

  4. 4

    在WPF中动态添加文本框

  5. 5

    在HTML中动态添加文本框

  6. 6

    动态添加时删除标记为按钮的文本框

  7. 7

    如何使用动态单选按钮添加事件以显示文本框

  8. 8

    使用实体框架,我想将数据添加到文本框中

  9. 9

    在jsp中动态添加<form:input>文本框

  10. 10

    在动态生成的文本框中添加on key up事件

  11. 11

    在VB.NET中动态添加文本框

  12. 12

    格式化在Excel中动态添加的文本框

  13. 13

    在BIRT报告中添加文本框并提交按钮

  14. 14

    单击Angular JS中的按钮添加新文本框

  15. 15

    通过单击按钮使用javascript添加文本框

  16. 16

    使用javascript在按钮单击上添加文本框

  17. 17

    如何使用C#在gridview中读取动态添加的文本框值?

  18. 18

    使用Selenium从只读文本框中获取动态文本

  19. 19

    如何使用实体框架将 WPF 文本框中的新文本值添加到数据库?

  20. 20

    我正在尝试使用JavaScript在按钮的onclick的有序列表中添加文本框,并在添加文本框时增加ID名称

  21. 21

    如何删除动态添加的文本框?

  22. 22

    通过绑定动态添加文本框

  23. 23

    javascript以动态添加文本框

  24. 24

    从文本框中动态获取文本

  25. 25

    使用C#中的按钮清除多个文本框

  26. 26

    使用按钮数组从文本框数组中获取价值

  27. 27

    使用按钮更新templateField中的文本框

  28. 28

    如何使用Java禁用单选按钮中的文本框

  29. 29

    单击按钮可更改动态生成的焦点文本框中的文本

热门标签

归档