How to disable touch screen selection in a list item in Windows Phone 7?

In the XAML template, I used a list to populate some dynamic data.

Now I want to disable the touch selection in some list items in Windows Phone 7. How to do this?

I did a little research, some said that a selection event could be prevented on the list.

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/8eeed347-ede5-4a24-88f1-953acd16e774

Hope some smart guys can teach me how to solve the problem.

Thanks.

+4
source share
4 answers

You can use ItemsControl instead of ListBox. ItemsControl is similar to a ListBox, but it comes without a choice.

<ItemsControl> <ItemsControl.ItemTemplate> <DataTemplate> ... </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> 
+10
source

Use ItemsControl wrapped in a ScrollViewer. This will give you the same effect without highlighting (and allow you to scroll as a ListBox does)

 <ScrollViewer> <ItemsControl> <ItemsControl.ItemTemplate> <DataTemplate> ... </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </ScrollViewer> 
+1
source

Try using a variable to store the last selected index.

If the user then selects the item that you do not want to handle the selection event, then set the selected index back to the stored value. If you want to transfer, e change the selection event, be sure to update the stored value.

You probably also want to avoid switching selections if you manually set the index (back).
You can use a simple Boolean flag to track this.

0
source

An old question, but there is no answer. You can do this with code by manipulating the selected item and index, but it is ugly and cumbersome. Instead, do it declaratively (XAML path!) With your related elements.

First you need a ViewModel with a list of items. Each element requires (at a minimum) a property to display and a property to determine whether the element is included or not.

Here's an example view model for one item in a list:

 class MyViewModel : ViewModelBase { private string _title; public string Title { get { return _title; } set { if(value == _title) return; _title = value; RaisePropertyChanged("Title"); } } private bool _isEnabled; public bool IsEnabled { get { return _isEnabled; } set { if(value == _isEnabled) return; _isEnabled = value; RaisePropertyChanged("IsEnabled"); } } } 

The above example assumes that MVVM Light is for ViewModelBase and the RaisePropertyChanged method, but you can do this using IPropertyNotified (or any other MVVM library).

You will then have a markup list similar to the following:

 <ListBox ItemsSource="{Binding MyItems}"> <ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <ContentPresenter IsHitTestVisible="{Binding IsEnabled}"/> </ControlTemplate> </Setter.Value> </Setter> </Style> </ListBox.ItemContainerStyle> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Title}"/> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

What is it. Now just load some viewmodel with a list of elements:

 MainViewModel.MyItems = new ObservableCollection<MyViewModel>(); MainViewModel.MyItems.Add(new MyViewModel { Title = "Star Wars", IsEnabled = true }); MainViewModel.MyItems.Add(new MyViewModel { Title = "The Sound of Music", IsEnabled = false }); MainViewModel.MyItems.Add(new MyViewModel { Title = "Aliens", IsEnabled = true }); MainViewModel.MyItems.Add(new MyViewModel { Title = "Debbie Does Dallas", IsEnabled = false }); MainViewModel.MyItems.Add(new MyViewModel { Title = "True Grit", IsEnabled = false }); 

In this example, only science fiction films are available.

Hope this helps.

0
source

All Articles