Binding to CurrentItem in a ItemsControl

前端 未结 2 594
难免孤独
难免孤独 2021-01-20 07:32

The XAML below is basically trying to make a list of Buttons (rendered from the Name property of objects in the Views collection in th

2条回答
  •  独厮守ぢ
    2021-01-20 08:02

    I think you have to do it manually in code-behind.

    XAML

    
    

    Code-behind

    private void ViewListButton_Click(object sender, RoutedEventArgs e)
    {
        Button button = sender as Button;
        ICollectionView view = CollectionViewSource.GetDefaultView(ViewList.ItemsSource);
        view.MoveCurrentTo(button.DataContext);
    }
    

    If you're using the MVVM pattern and/or you don't want to use code-behind, you could bind the button's Command to a command in your ViewModel, as suggested by Will

提交回复
热议问题