WPF和模式MVVM如何从文本框中获取值,将其发送到ViewModel并保存dto模型

时间:2015-08-02 06:24:51

标签: c# wpf mvvm

我是WPF和MVVM的新手。我读了很多关于WPF命令的文章,但是我仍然有问题从textbox的属性文本发送值到ViewModel。 我首先使用实体​​框架代码。

我想在MessageBox中显示文本框中的文本,但是当我用命令单击按钮时,viewmodel的链接属性为null。 请你能帮帮我吗?

查看 - DetailIncidentWindow.xaml

xmlns:wm="clr-namespace:Admin.ViewModels"
<StackPanel>
                <StackPanel.DataContext>
                    <wm:CommentViewModel/>
                </StackPanel.DataContext>
            <TextBlock Text="Text komentáře:" Style="{StaticResource TextBlockLabel}" Margin="0,10,0,0"/>
                <TextBox Name="TextBox_textKomentar" Width="auto" Height="100" TextWrapping="Wrap" Text="{Binding TextKomentar, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                <TextBlock Text="{Binding TextKomentar, UpdateSourceTrigger=PropertyChanged}"/>
            </StackPanel>

功能区按钮 - DetailIncidentWindow.xaml

<Custom:RibbonGroup.DataContext>
                        <wm:CommentViewModel/>
                    </Custom:RibbonGroup.DataContext>
                    <Custom:RibbonButton
                        LargeImageSource="..\Shared\img\save_diskete.png"
                        Label="Show text"
                        Command="{Binding ButtonCommand}">
                    </Custom:RibbonButton>

ViewModel- KomentarViewModel.cs

namespace Admin.ViewModels
{
    class CommentViewModel:BaseViewModel
    {
        #region Data
        private string textKomentar;
        public string TextKomentar
        {
            get
            {
                return textKomentar;
            }
            set
            {
                textKomentar = value;
                OnPropertyChanged("TextKomentar");
            }
        }

        private ICommand m_ButtonCommand;
        public ICommand ButtonCommand
        {
            get
            {
                return m_ButtonCommand;
            }
            set
            {
                m_ButtonCommand = value;
                OnPropertyChanged("ButtonCommand");
            }
        }

        #endregion

        #region Constructor
        public CommentViewModel()
        {
            ButtonCommand = new RelayCommand(new Action<object>(ShowMessage));            
        }
        #endregion

        #region Methods

        public void ShowMessage(object obj)
        {
            MessageBox.Show(TextKomentar);
        }
        #endregion
    }
}

Command-RelayCommand.cs

class RelayCommand : ICommand
    {
        private Action<object> _action;

        public RelayCommand(Action<object> action)
        {
            _action = action;
        }

        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            if (parameter != null)
            {
                _action(parameter);
            }
            else
            {
                _action("Hello World");
            }
        }

        #endregion
    }

1 个答案:

答案 0 :(得分:0)

您不应像在

中那样创建视图模型的多个实例
<StackPanel.DataContext>
    <wm:CommentViewModel/>
</StackPanel.DataContext>

<Custom:RibbonGroup.DataContext>
    <wm:CommentViewModel/>
</Custom:RibbonGroup.DataContext>

DataContext属性的值由子元素继承,因此您可以将其设置在顶层,例如窗口:

<Window ...>
    <Window.DataContext>
        <wm:CommentViewModel/>
    </Window.DataContext>
    ...
</Window>