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

C# WPF和模式MVVM如何从文本框中获取值,将其发送到ViewModel并将dto保存到model,c#,wpf,mvvm,C#,Wpf,Mvvm,我是WPF和MVVM的新手。我读了很多关于WPF命令的文章,但在将值从textbox的属性文本发送到ViewModel时仍然存在问题。 我首先使用实体框架代码 我想在MessageBox中显示textbox中的文本,但当我用命令单击按钮时,viewmodel的链接属性为null。 你能帮我吗 查看-DetailIncidentWindow.xaml xmlns:wm="clr-namespace:Admin.ViewModels" <StackPanel>

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

我想在MessageBox中显示textbox中的文本,但当我用命令单击按钮时,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>
xmlns:wm=“clr命名空间:Admin.ViewModels”
功能区按钮-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
    }
}
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
    }
namespace Admin.ViewModels
{
类CommentViewModel:BaseViewModel
{
#区域数据
私有字符串textKomentar;
公共字符串TextKomentar
{
得到
{
返回textKomentar;
}
设置
{
textKomentar=值;
OnPropertyChanged(“TextKomentar”);
}
}
专用ICommand和m_按钮命令;
公用ICommand按钮命令
{
得到
{
返回m_按钮命令;
}
设置
{
m_按钮命令=值;
关于财产变更(“按钮命令”);
}
}
#端区
#区域构造函数
公共评论视图模型()
{
ButtonCommand=新的RelayCommand(新操作(显示消息));
}
#端区
#区域方法
公共无效显示消息(对象obj)
{
MessageBox.Show(TextKomentar);
}
#端区
}
}
命令-RelayCommand.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
    }
}
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
    }
类RelayCommand:ICommand
{
私人行动;
公共关系司令部(行动)
{
_行动=行动;
}
#区域ICommand成员
公共布尔CanExecute(对象参数)
{
返回true;
}
公共事件处理程序CanExecuteChanged;
public void Execute(对象参数)
{
if(参数!=null)
{
_作用(参数);
}
其他的
{
_行动(“你好世界”);
}
}
#端区
}

您不应该像在中那样创建视图模型的多个实例

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


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

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

...

您不应该像在中那样创建视图模型的多个实例

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


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

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

...