簡體   English   中英

使用ItemsControl中TextBlock的值過濾ListBox中的Collection

[英]Filter Collection in ListBox using value of TextBlock in ItemsControl

我創建了自己的日歷。 我日歷中的每一天都有一個itemsControl,其中包含一個文本塊和一個列表框,該列表框應按日期保存項目。

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

視圖模型

 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>

首先,您的ListBox的ItemsSource綁定將不起作用,因為它的DataContext是Day對象,並且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;
}

ViewModel(示例),在您的Days集合初始化中:

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM