wpf SelectedIndex CommandParameter

时光怂恿深爱的人放手 提交于 2019-12-11 10:25:02

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/9357763/wpf-selectedindex-commandparameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!