2015-07-20 30 views
1

我創建了自己的日曆。我的日曆中的每一天都是一個itemsControl,它包含一個文本塊和一個列表框,它應該包含每個日期的項目。使用ItemsControl中TextBlock的值在ListBox中過濾集合

如何使用ItemsControl中綁定文本塊的字符串值過濾集合? 文本塊與Day類的日期屬性綁定。

視圖模型

public ObservableCollection<Day> Days { get; set; } 

public ObservableCollection<Scene> SceneList; 

private ListCollectionView _sceneCollection; 
public ListCollectionView SceneCollection 
{ 
    get 
    { 
     if (_sceneCollection == null) //important for loading the app 
     { 
      _sceneCollection = new ListCollectionView(this.SceneList); 
      _sceneCollection.IsLiveFiltering = true; 
      _sceneCollection.Filter = o => 
      { 
       var Scene = o as Scene; 
       return Scene != null && Scene.Date == ////string of binded TextBlock//; 
      }; 
     } 
     return _sceneCollection; 
    } 
    set 
    { 
     _sceneCollection = value; RaisePropertyChanged(); 
    } 
} 

型號

public class Day : INotifyPropertyChanged 
{ 
    private DateTime date; 
    public DateTime Date 
    { 
     get { return date; } 
     set 
     { 
      date = value; 
      if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Date")); 
     } 
    } 
} 

的XAML

<ItemsControl ItemsSource="{Binding Days}">   
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <UniformGrid Rows="6" Columns="7">      
       </UniformGrid>     
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 

     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <StackPanel> 
        <TextBlock Text="{Binding Date , Converter={StaticResource DateConverter}, ConverterParameter=DAY}"/>      
        <ListBox ItemsSource="{Binding SceneCollection}" dd:DragDrop.IsDragSource="True" 
    dd:DragDrop.IsDropTarget="True" Height="100"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel> 
           <TextBlock> 
            <Run Text="{Binding Path=SceneNumber}"/> 
            <Run Text="{Binding Path=SlugLine}"/> 
           </TextBlock> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
        </ListBox> 
       </StackPanel> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 

回答

1

所有,你的ListBox的ItemsSource綁定將無法工作,因爲它的DataContext是天對象,並且第一SceneCollection屬性不存在,但在您的ViewModel中。

此外,你不應該過濾你的ViewModel中的集合,因爲所有的項目都將綁定到它,他們將需要不同的過濾器。

在你的情況下,如果你想要在保持底層集合完好的情況下使用過濾器和集合視圖,我只需在'Day'類中添加一個'ICollectionView'屬性,併爲每一天分配一個你的SceneCollection的過濾視圖。

型號:

public class Day : INotifyPropertyChanged 
{ 
    private DateTime date; 
    public DateTime Date 
    { 
     get { return date; } 
     set 
     { 
      date = value; 
      if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Date")); 
     } 
    } 

    private ICollectionView scenes; 
    public ICollectionView Scenes 
    { 
     get { return scenes; } 
     set 
     { 
      scenes = value; 
      if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Scenes")); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

視圖模型(例如),在你的日子集合初始化:

private IEnumerable<Day> CreateDaysData() 
{ 
    var maxDays = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month); 

    for (int d = 1; d <= maxDays; d++) 
    { 
     var day = new Day 
     { 
      Date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, d) 
     }; 

     var viewSource = new CollectionViewSource 
     { 
      Source = ScenesCollection 
     }; 

     viewSource.Filter += new FilterEventHandler((o, e) => 
     { 
      e.Accepted = (e.Item as Scene).Date == day.Date; 
     }); 

     day.Scenes = viewSource.View; 

     yield return day; 
    } 
} 

最後,你的XAML最終會是這樣的:

<ItemsControl ItemsSource="{Binding Days}">   
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <UniformGrid Rows="6" Columns="7">      
      </UniformGrid>     
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 

    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <TextBlock Text="{Binding Date , Converter={StaticResource DateConverter}, ConverterParameter=DAY}"/>    
       <!-- The ListBox's ItemsSource is bound to the ICollectionView of your Day class -->   
       <ListBox ItemsSource="{Binding Scenes}" dd:DragDrop.IsDragSource="True" 
dd:DragDrop.IsDropTarget="True" Height="100"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel> 
          <TextBlock> 
           <Run Text="{Binding Path=SceneNumber}"/> 
           <Run Text="{Binding Path=SlugLine}"/> 
          </TextBlock> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
       </ListBox> 
      </StackPanel> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl>