标签: collectionviewsource

WPF如何从CollectionViewSource设置最大结果数

我想显示一组使用数据的DataTemplateItemsControlItemsPanel集作为大小3×3的均匀网格的ItemsSource所述的ItemsControl被设置为结合至CollectionViewSource其过滤基于搜索项的源集合。这一切都很好。

我绑定到的列表是任意大小的,但是我只希望显示9个结果,但是我一生都无法解决如何:
a)限制 CollectionViewSource输出前9个项目
b)将限制UniformPanel为仅3 x 3,并且从不创建新行
c)将限制ItemsControl为仅一次创建9个数据模板。

我真的挠头,因为我确定这是一个常见的数据绑定方案,但是我在网上找不到任何有关它的信息。

c# data-binding wpf itemscontrol collectionviewsource

5
推荐指数
1
解决办法
2656
查看次数

你可以在DataTemplate中使用CollectionViewSource吗?

是否可以在数据模板中显式使用CollectionViewSource?通常我们将CollectionViewSource放在模板旁边的资源中,但我们的模型不允许这样做,因为collectionviewsource的'source'是树中此级别的DataContext的属性,这意味着需要有一个实例在这个水平.把它放在资源的根部就意味着只有一个实例.我们也不能简单地在外层使用分组,因为这些项目不存在,直到您在层次结构中这么远,并且并非所有兄弟都拥有此属性.因此,逻辑上我们在DataTemplate中实例化CollectionViewSource(在这个例子中是HierarchicalDataTemplate,但这是无关紧要的.)

具体来说,我们尝试在此特定节点级别允许特定排序.我们唯一的另一种选择是在ViewModel本身进行排序,但由于我们使用的ObservableCollections本身不支持排序,因此会变得很痛苦.实际上,我们在这个主题上看到的每篇文章都说明你应该正是因为这个原因使用CollectionViewSource,因此这个问题.

例如,这有效......

<HierarchicalDataTemplate x:Key="CategoryTemplate"
    ItemTemplate="{StaticResource TreeViewSymbolTemplate}"
    ItemsSource="{Binding Symbols}">

    <TextBlock Text="{Binding Name}" FontWeight="Bold" />

</HierarchicalDataTemplate>
Run Code Online (Sandbox Code Playgroud)

但这不......

<HierarchicalDataTemplate x:Key="CategoryTemplate"
    ItemTemplate="{StaticResource TreeViewSymbolTemplate}">

    <HierarchicalDataTemplate.ItemsSource>
        <Binding>
            <Binding.Source>
                <CollectionViewSource Source="{Binding Symbols}" />
            </Binding.Source>
        </Binding>
    </HierarchicalDataTemplate.ItemsSource>

    <TextBlock Text="{Binding Name}" FontWeight="Bold" />

</HierarchicalDataTemplate>
Run Code Online (Sandbox Code Playgroud)

在我看来它会像它一样,但事实并非如此.同样,我们不能将CollectionViewSource放在与数据模板相同的级别,因为每个模板需要一个实例,因为每个模板都有自己的一组项目(尽管它们都将共享排序条件.)

中号

data-binding wpf datatemplate observablecollection collectionviewsource

5
推荐指数
1
解决办法
1298
查看次数

使用CollectionViewSource的组的自定义排序逻辑

我有一个"东西"列表,需要过滤,然后以分组和排序的方式显示.计算分组和排序顺序所需的数据不能作为简单属性使用 - 需要在代码中完成一些工作来计算顺序和组.

CollectionViewSource允许我定义自定义过滤器和排序逻辑 - 到目前为止一切都很好.它还允许我将GroupDescriptions绑定到值转换器,以便我可以生成组名称.

我要做的最后一件事是控制生成的组出现的顺序,这让我感到痛苦!

我看到的关于CollectionViewSource.SortDescriptions的所有内容都表示它将按属性名称对组进行排序,但我没有可用于排序的属性.SortDescriptions不能绑定到像GroupDescriptions这样的值转换器,而且我没有其他想法.

那么 - 如何实现CollectionViewSource组的自定义排序逻辑?

wpf grouping collectionviewsource

4
推荐指数
1
解决办法
4466
查看次数

如何从 CollectionViewSource 添加或删除项目?

我想将 datagrid.itemsource 绑定到具有匿名类型的列表,因此我将其绑定到 collectionViewSource,但我需要向列表添加或删除项目,但我不知道如何?

wpf binding list collectionviewsource

4
推荐指数
1
解决办法
8673
查看次数

How to synchronize ListCollectionView.CurrentItem and ListView.SelectedItem?

I have a ViewModel containing a ListCollectionView property, and a View containing a ListView control whose ItemsSource property is data bound to the ListCollectionView:

ViewModel:

    public ListCollectionView Pacientes { get; set; }

    public IRepositório<Paciente> RepositórioPacientes { get; set; }


    // CONSTRUTOR
    public MainViewModel()
    {
        RepositórioPacientes = new PacienteRepositorioFake();

        Pacientes = (ListCollectionView)CollectionViewSource.GetDefaultView(RepositórioPacientes.Items);
    }
}
Run Code Online (Sandbox Code Playgroud)

View (heavily stripped down):

    <ListView ItemsSource="{Binding Pacientes}"/>
    <Border DataContext="{Binding Pacientes/}">
        <!-- some controls displaying properties of Paciente entity -->
    </Border>
Run Code Online (Sandbox Code Playgroud)

Note the Binding Pacientes/ with a trailing …

data-binding wpf listview listcollectionview collectionviewsource

4
推荐指数
1
解决办法
1915
查看次数

错误:此类型的CollectionView不支持对其SourceCollection的更改

我有一个ObservableCollection项,我需要能够更新并使用ICollectionView表示数据.

以下是相关的代码:

private ObservableCollection<Hero> heroesDBHeroes;
public ObservableCollection<Hero> HeroesDBHeroes
{
    get
    {
        return heroesDBHeroes;
    }
    set
    {
        heroesDBHeroes = value;
        OnPropertyChanged("HeroesDBHeroes");
    }
}
private void HeroesDBAddHeroes()
{
    if(HeroesDBHeroes != null)
    {
        HeroesDBHeroes.Clear();
    }
    HeroesDBHeroes = Hero.GetAllHeroes();

    HeroesDBFilteredHeroes = new ListCollectionView(HeroesDBHeroes);
    HeroesDBFilteredHeroes.Filter = new Predicate<object>(HeroesDBFilterHeroes);
    HeroesDBFilteredHeroes.Refresh();
    OnPropertyChanged("HeroesDBFilteredHeroes");
}
Run Code Online (Sandbox Code Playgroud)

这是CollectionView及其过滤器:

    public CollectionView HeroesDBFilteredHeroes { get; set; }
    public bool HeroesDBFilterHeroes(object item)
    {
        Hero h = item as Hero;
        bool ID, Name, GoldMinimum, GoldMaximum, PlatinumMinimum, PlatinumMaximum, DBTag, ReleaseDateStart, ReleaseDateEnd, Available, Sale, Featured, New, F2P, …
Run Code Online (Sandbox Code Playgroud)

c# wpf observablecollection mvvm collectionviewsource

4
推荐指数
1
解决办法
6932
查看次数

Bind a Wpf HierarchicalDataTemplate ItemsSource to a CollectionViewSource in a dictionary?

I'm trying to display a Wpf Treeview with items sorted by a CollectionViewSource.

Currently, everything is working except sorting using this code in my resource dictionary:

<HierarchicalDataTemplate DataType="{x:Type books:Container}" ItemsSource="{Binding Path=Items}">
    <nav:ContainerControl />
</HierarchicalDataTemplate>
Run Code Online (Sandbox Code Playgroud)

What would be the syntax for changing the HierarchicalDataTemplate to bind to a CollectionViewSource that in turn pulls from the Items property?

I've tried variations of the code posted on Bea Stollnitz's blog with no success. I can't figure out how to set the source of …

data-binding wpf hierarchicaldatatemplate collectionviewsource

3
推荐指数
1
解决办法
4691
查看次数

CollectionViewSource + PropertyGroupDescription - 组中项目的数量

在我的WPF应用程序中,我有一个CollectionViewSource,它提供了一个私有ObservableCollection的视图.CollectionViewSource有一个PropertyGroupDescription,它在ListBox中用于向用户的首选项显示数据.

在ListBox GroupStyle中使用包含Expander Control的ControlTemplate,结果非常好.但是,除了Group Name之外,我想在Expander Header中显示每个组中的项目数.关于绑定路径的任何想法?

此致,利亚姆

<Style x:Key="basicGroupStyle" TargetType="{x:Type GroupItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupItem}">
                <Expander Header="{Binding Name}" IsExpanded="True">
                    <ItemsPresenter/>
                </Expander>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<ListBox ItemsSource="{Binding Source={StaticResource myViewSource}}">
    <ListBox.GroupStyle>
         <GroupStyle ContainerStyle="{StaticResource basicGroupStyle}"/>
    </ListBox.GroupStyle>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

wpf grouping collectionviewsource

3
推荐指数
1
解决办法
6256
查看次数

嵌套对象的绑定路径

我刚刚掌握数据绑定,我正在努力绑定到嵌套在对象下方的ObservableCollection中的属性,即在ListView的DataTemplate中我试图绑定到下面的Day.DayDate属性.

它是一个日记应用程序,这是它的结构(编辑,以保持简短):

public class Month : INotifyPropertyChanged
{
    public DateTime StartDate { get; set; }
    public ObservableCollection<Day> Days { get; set; }
}

public class Day : INotifyPropertyChanged
{
    public DateTime DayDate { get; set; }
    public ObservableCollection<Gig> Gigs { get; set; }
}

public class Gig : INotifyPropertyChanged
{
    // Properties of a gig
}
Run Code Online (Sandbox Code Playgroud)

我最初填充像这样的月份日:

private void InitMonth(Month calendarMonth)
{
    // create a Day Object for each day of month, create a gig for each booking on that …
Run Code Online (Sandbox Code Playgroud)

data-binding wpf datatemplate collectionviewsource

3
推荐指数
1
解决办法
7214
查看次数

如何将WPF CollectionViewGroup类型更改为自定义类型并在ListCollectionView中使用它

如何将View属性的类型重写为我的自定义类型。我的CustomGroupListCollectionView类型为Groups属性添加了额外的属性。在运行时,当我观察到View属性的类型为ListCollectionView时,我想将其更改为CustomGroupListCollectionView。

public class CollectionViewSourceCustom : CollectionViewSource
{
    public new  CustomGroupListCollectionView View { get; set; }
}

public class CustomGroupListCollectionView : ListCollectionView
{
    private readonly CustomGroup _allGroup;

    public CustomGroupListCollectionView(IList list)
        : base(list)
    {
        _allGroup = new CustomGroup("All");
        foreach (var item in list)
        {
            _allGroup.AddItem(item);
        }
    }

    public override ReadOnlyObservableCollection<object> Groups
    {
        get
        {
            var group = new ObservableCollection<object>(base.Groups.ToList());
            group.Add(_allGroup);
            return new ReadOnlyObservableCollection<object>(group);
        }
    }
}

public class CustomGroup : CollectionViewGroup
{
    public CustomGroup(object name)
        : base(name)
    {
    }

    public void AddItem(object …
Run Code Online (Sandbox Code Playgroud)

c# wpf collectionviewsource

3
推荐指数
1
解决办法
2008
查看次数