【问题标题】:wp7 listbox selectionwp7列表框选择
【发布时间】:2013-04-03 06:29:32
【问题描述】:

我有一个包含 1000 多个项目的列表框。

在xml中

<ListBox 
//some code here
SelectionChanged="OnSelectionChanged">
    <ListBox.ItemTemplate>
         <DataTemplate>
             <controls:MyCustomItem/>
         </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

在cs中

 private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var item = lb.SelectedItem as MyCustomItem;
        var vm = DataContext as ViewModel;
        if (vm == null) return;
        foreach (var it in vm.ItemsForBinding)
        {
            it.IsSelected = false;
        }
        item .IsSelected = true;
    }

在 MyCustomItem xaml 中

<UserControl
//Some code here
Style="{Binding Path=IsSelected, Converter = {StaticResource BoolToStyle}}">
    <Border 
    </Border>
</UserControl>

Where IsSelected - 我的 ViewModel 的属性之一。转换器返回两种样式中的一种(第一个如果选中项目,第二个 - 如果没有)

总是有效,但我知道 - 这非常耗费资源,而且是错误的决定。 怎么做才对?

【问题讨论】:

  • 从观点来看,主要问题是列表框中有 1000 多个项目。您真的需要列表框中的那么多项目吗?你能把它分解成多个列表框吗?
  • 不,我真的需要在单个 ListBox 上显示所有具有过滤功能的项目。我找到了解决方案并发布了它。

标签: windows-phone-7 windows-phone-8 windows-phone windows-phone-7.1.1


【解决方案1】:

这个post帮帮我! 只需覆盖我的 ListBox 的默认 ItemContainerStyle。

我删除了:

metod OnSelectionChanged、UserControl MyCustomItem、样式转换器和

<ListBox.ItemTemplate>
     <DataTemplate>
         <controls:MyCustomItem/>
     </DataTemplate>
</ListBox.ItemTemplate>

来自 xaml。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多