Windows phone 7 如何在Windows Phone 7中禁用列表框项目中的触摸选择?
在XAML模板中,我使用了一个列表框来填充一些动态数据 现在我想在WindowsPhone7中的某些列表框项目中禁用触摸选择。怎么做 我做了一些小调查,有人说可以在列表框中阻止选择事件 希望一些聪明人能教我如何解决这个问题Windows phone 7 如何在Windows Phone 7中禁用列表框项目中的触摸选择?,windows-phone-7,Windows Phone 7,在XAML模板中,我使用了一个列表框来填充一些动态数据 现在我想在WindowsPhone7中的某些列表框项目中禁用触摸选择。怎么做 我做了一些小调查,有人说可以在列表框中阻止选择事件 希望一些聪明人能教我如何解决这个问题 谢谢。尝试使用变量存储上次选择的索引 如果用户随后选择了一个您不想为其处理选择事件的项目,则将所选索引设置回存储值。如果你真的想举手;e选择更改事件,然后确保也更新存储的值 手动设置索引(返回)时,您可能还希望避免触发更改的选择。 一个简单的布尔标志可以用来跟踪这个问题。老
谢谢。尝试使用变量存储上次选择的索引 如果用户随后选择了一个您不想为其处理选择事件的项目,则将所选索引设置回存储值。如果你真的想举手;e选择更改事件,然后确保也更新存储的值 手动设置索引(返回)时,您可能还希望避免触发更改的选择。
一个简单的布尔标志可以用来跟踪这个问题。老问题,但看起来没有得到回答。您可以通过代码来完成,操作选定的项和索引,但这既丑陋又麻烦。相反,让我们以声明的方式(XAML方式!)对绑定的项执行此操作 首先,您需要一个带有项目列表的ViewModel。每个项目都需要(至少)一个要显示的属性和一个确定项目是否已启用的属性 以下是列表中单个项目的示例viewmodel:
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");
}
}
}
上面的示例假设ViewModelBase和RaisePropertyChanged方法使用MVVM Light,但您可以使用IPropertyNotified自己(或任何其他MVVM库)来实现
接下来,您将看到一个具有类似以下标记的列表框:
<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>
就这样。现在只需加载一些带有项目列表的viewmodel:
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 });
MainViewModel.MyItems=new ObservableCollection();
MainViewModel.MyItems.Add(新的MyViewModel{Title=“Star Wars”,IsEnabled=true});
MainViewModel.MyItems.Add(新的MyViewModel{Title=“音乐之声”,IsEnabled=false});
MainViewModel.MyItems.Add(新的MyViewModel{Title=“异形”,IsEnabled=true});
MainViewModel.MyItems.Add(新的MyViewModel{Title=“Debbie Dos Dallas”,IsEnabled=false});
MainViewModel.MyItems.Add(新的MyViewModel{Title=“True Grit”,IsEnabled=false});
只有本例中的科幻电影是可点击的
希望对您有所帮助。您可以使用ItemsControl而不是ListBox。ItemsControl类似于ListBox,但它没有选择内容
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
...
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
...
使用ScrollViewer中包装的ItemsControl。这将给你同样的效果,无需选择(并允许你像列表框一样滚动)
...
对我来说非常有用(不需要重新设置列表框的样式),他问了一些lisboxitem
<ScrollViewer>
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
...
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>