1
votes

i'm new in WPF and MVVM. I read many articles about WPF commands, but i have still problem with sending value from property text of textbox to ViewModel. I'm using entity framework code first.

I want to show text from textbox in MessageBox, but when I click to button with command, linked property of viewmodel is null. Please can you help me?

View- 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>

Ribbon button- 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

1 Answers

0
votes

You should not create multiple instances of your view model, like you do in

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

and

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

The value of the DataContext property is inherited by child elements, so you could just set it at the top level, e.g. the Window:

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