2015-02-09 92 views
0

后,我有一个开发快递(MVVM)复选框 - 列表框编辑(与卡利微型),允许多个选择,我试图搜索功能附加到,这是工作如预期的那样,但是当我选择了项目,然后搜索时,先前选择的项目将会丢失。我的继承人XAML:保持选定项目重绘箱

<layout:LayoutItem Label="label : " Foreground="White" LabelPosition="Top"> 
      <DockPanel> 
       <TextBox Text="{Binding Path=SeachItems, UpdateSourceTrigger=PropertyChanged}" DockPanel.Dock="Top" Width="400"> 
       </TextBox> 

       <dxe:ListBoxEdit DockPanel.Dock="Bottom" Foreground="Black" Margin="10" Width="400" DisplayMember="Name" MaxHeight="200" MinHeight="200" ItemsSource="{Binding Path=Items}" EditValue="{Binding Path=SelectedItems}" SelectionMode="Multiple" > 
        <dxe:ListBoxEdit.StyleSettings> 
         <dxe:CheckedListBoxEditStyleSettings /> 
        </dxe:ListBoxEdit.StyleSettings> 
       </dxe:ListBoxEdit> 
      </DockPanel> 
     </layout:LayoutItem> 

列表框(我想这可能是我的问题的一部分,因为我不能想出一个办法,以我的选择绑定到ObservableCollection,我只能seemt绑定的声明他们一个泛型列表):

public ObservableCollection<Items> Items { get; set; } 
public List<object> SelectedItems { get; set; } 

这里是我的搜索方法:

private string _searchItems; 
public string SeachItems 
    { 
     get { return _searchItems; } 
     set 
     { 
      _searchItems = value; 
      var tempItems = SelectedItems; 
      var items = //fetch all items from collection; 
      Items = (from p in items where p.Name.ToLower().StartsWith(SeachItems.ToLower()) orderby p.Name select p).ToObservableCollection(); 

      NotifyOfPropertyChange(()=>Items); 
      SelectedItems = tempItems; 
      NotifyOfPropertyChange(()=>SelectedItems); 

     } 
    } 

虽然SelectedItems在调用确实有计,在查看实际列表框时,没有项目被选中,并且下一个搜索清除了SelectedItems任何指针?

编辑:

在设置模式,我填充ItemsSelectedItems

Items = //populate observable collection from database 
SelectedItems = new List<object>(); 
+0

您应该使用视图来进行筛选,而不是实际替换项目集合。目前还不清楚你声明的Items属性是什么,但如果这是ListBoxEdit属性,那么将对象的Items属性绑定到它自己的ItemsSource属性是很奇怪的。您发布的代码中有很多提示“做错了”,但没有[一个很好的,_minimal_,_complete_代码示例](http://stackoverflow.com/help/mcve)来说明您的问题,这不是真的可能提供具体的建议。 – 2015-02-10 03:35:58

+0

@PeterDuniho从我的设置模型中添加了基本代码,说实话,这可能是我做错了,这就是为什么我张贴在这里,寻求帮助。 – Typhomism 2015-02-10 03:41:38

回答

0

上午创建实例时,您应该CollectionViewSource筛选项目。例如Here就是例子。 非常基本:

ICollectionView myCollectionVIew = CollectionViewSource.GetDefaultView(items); 
myCollectionVIew.Filter = p => { return p.Name.ToLower().StartsWith(SeachItems.ToLower(); }; 
0

您可以简单地绑定选定的值。重绘时,值将相同。

<dxe:ListBoxEdit DockPanel.Dock="Bottom" Foreground="Black" Margin="10" 
        Width="400" DisplayMember="Name" MaxHeight="200" MinHeight="200" 
        ItemsSource="{Binding Path=Items}" 
        EditValue="{Binding Path=SelectedItems}" 
        SelectionMode="Multiple" 
        SelectedItem = {"Binding myNewVar"}>