wpf SelectedIndex CommandParameter

时间:2012-02-20 07:50:15

标签: wpf commandparameter

按钮的Command是ExcelExportCommand,其CommandParameter类似于:

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

如何以编程方式通过ViewModel获取SelectedIndex?我是MVVM模式的新手,我想验证我采取了正确的方法。你能帮忙吗?

提前致谢

1 个答案:

答案 0 :(得分:1)

您可以将ListTabControl的SelectedIndex属性绑定到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
    }
}