Search code examples
wpfcommandparameter

wpf SelectedIndex CommandParameter


A button's Command is ExcelExportCommand and its CommandParameter is like:

<Button x:Name="ExcelExport" Grid.Row="1" Height="25" Width="100" Command="{Binding ExcelExportCommand}" CommandParameter="{Binding ElementName=ListTabControl, Path=SelectedIndex}">Export to Excel</Button>

How can i get the SelectedIndex through a ViewModel programmatically? I'm new to MVVM pattern and I want to verify that I had taken the right approach. Can you help?

Thanks in advance


Solution

  • You can bind the SelectedIndex property of your ListTabControl to an integer property in your viewmodel:

    <List x:Name="ListTabControl" SelectedIndex="{Binding ListSelectedIndex}" />

    private int _ListSelectedIndex;
    public int ListSelectedIndex {
        get { return _ListSelectedIndex;}
        set
        {
            _ListSelectedIndex = value;
            OnPropertyChanged("ListSelectedIndex"); // if INotifyPropertyChanged implemented
        }
    }