E
E
ette2016-11-02 15:05:55
WPF
ette, 2016-11-02 15:05:55

C# WPF MVVM how to get a reference to the object that executed the bind command?

I am learning WPF, I recently got acquainted with the MVVM pattern and a question arose while studying.
Suppose there is a control in the markup, there is a button with a command binding. There is a certain task that needs to be solved - switching between tabs of the TabControl by clicking on the button, changing other controls located inside the TabItem.
For example, there is a markup:

<Window...>
<DockPanel>
<TabControl x:Name="tabControl" ItemsSource="{Binding Tabs}"/>
</DockPanel>
</Window>

app.xaml:
<!--Для каждого экземпляра класса SuperTabOne:SuperTab -->
  <DataTemplate DataType="{x:Type local:SuperTabOne}">
                <StackPanel>
                    <Button Content="Перейти на вторую вкладку" Command="{Binding SelectTab}"></Button>
                </StackPanel> 
        </DataTemplate>

mainwindowviewmodel:
//коллекция классов, представляющих вкладки для TabControl аля SuperTabOne, SuperTabTwo, SuperTabEnd и др.
public ObservableCollection<SuperTab> Tabs
        {
            get { return _tabs ?? (_tabs = new ObservableCollection<SuperTab>()); }
        }
        private ObservableCollection<SuperTab> _tabs;

       RelayCommand _selectTabCommand;
        public ICommand SelectTab
        {
            get
            {
               if (_selectTabCommand == null)
                 _selectTabCommand = new RelayCommand(ExecuteSelectTabCommand, CanExecuteSelectTabCommand);
               return _selectTabCommand;
            }
        }


        public void ExecuteSelectTabCommand(object parameter)
        {
          //выбрать следующую вкладку
        }

        public bool CanExecuteSelectTabCommand(object parameter)
        {
           //логика...
            return true;
        }

How in the method of executing the command (ExecuteCommand) will address the control in order to perform any actions on it? Or, for example, how to refer to the element that triggered the event (executed the command on the binding) and should it be done? So far, this is not obvious to me (
Now I create different TabItem tabs by executing a certain AddTab command, I would like to implement other actions when executing the corresponding command (in ExecuteCommand):
1. Switching from one tab to another. I would like to understand how to find out which tab chosen and how to choose another.
2. Changing other controls that do not belong to TabControl. To be more precise, when you select a tab, for example, TextBoxes should appear on the right panel or, for example, buttons in the bottom panel - the status bar. When moving from tab to tab, TextBoxes will also change - for each tab - their own.
3. Closing the tab. In general, there are considerations on how this is done, but I would like to receive recommendations on this item as well.
How is this implemented in MVVM? Please describe the LOGIC for solving these tasks within the framework of MVVM WPF. If possible, give some code as an example, share a link to a project with a visual solution to similar problems, if you know any. Thank you in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tex0, 2016-11-02
@ette

How in the method of executing the command (ExecuteCommand) will address the control in order to perform any actions on it?

All interactions are through the application model.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question