开发者

How to disable touch selection in listbox item in Windows Phone 7?

开发者 https://www.devze.com 2023-01-23 02:19 出处:网络
In the XAML template, I used a listbox to populate some dynamic data. Now I want to disable touch selection in certain listbox items in Windows Phone 7. How to do that ?

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

Now I want to disable touch selection in certain listbox items in Windows Phone 7. How to do that ?

开发者_如何学C

I had done some little research, some people said that the selection event could be prevented in the listbox.

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

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

Thanks.


You could use the ItemsControl instead of ListBox. ItemsControl is like ListBox, but it comes without the selection stuff.

<ItemsControl>
   <ItemsControl.ItemTemplate>
      <DataTemplate>
      ...
      </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>


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

<ScrollViewer>
  <ItemsControl>
     <ItemsControl.ItemTemplate>
       <DataTemplate>
      ...
       </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
</ScrollViewer>


Try using a variable to store the last selected index.

If the user then selects an item which you don't want to handle the selection event for then set the selected index back to the stored value. If you do want to hand;e the selection changed event then be sure to update your stored value too.

You'll probably also want to avoid triggering the selection changed when you manually set the index (back).
A simple boolean flag could be used to track this.


Old question but it doesn't look answered. You can do it through code, manipulating the selected item and index, but that's ugly and cumbersome. Instead let's do it declaratively (the XAML way!) with your bound items.

First you'll need a ViewModel with a list of items. Each item needs (at minimum) a property to display and a property to determine if the item is enabled or not.

Here's a sample viewmodel for a single item in the 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 MVVM Light for the ViewModelBase and the RaisePropertyChanged method, but you can do it with IPropertyNotified yourself (or any other MVVM library).

Next you'll have a listbox with markup 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>

That's it. Now just load up some viewmodel with a list of items:

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

Only the sci-fi movies in this example are clickable.

Hope that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消