Answer the question
In order to leave comments, you need to log in
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>
<!--Для каждого экземпляра класса SuperTabOne:SuperTab -->
<DataTemplate DataType="{x:Type local:SuperTabOne}">
<StackPanel>
<Button Content="Перейти на вторую вкладку" Command="{Binding SelectTab}"></Button>
</StackPanel>
</DataTemplate>
//коллекция классов, представляющих вкладки для 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;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question