【问题标题】:Expand grouped ListBox when filtering through CollectionViewSource通过 CollectionViewSource 过滤时展开分组的 ListBox
【发布时间】:2016-07-27 20:33:40
【问题描述】:

我有一个工具箱(类似 Visual Studio)和一个用户可以过滤的文本框。过滤器正在工作,但 ListBox 中的项目始终保持未展开

View.xaml

<ListBox x:Name="ListBox" Grid.Row="1" ItemsSource="{Binding Items}" 
             PreviewMouseLeftButtonDown="OnListBoxPreviewMouseLeftButtonDown" MouseMove="OnListBoxMouseMove"
             Background="Transparent" BorderThickness="0" ScrollViewer.CanContentScroll="False">
        <ListBox.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <gem:ExpanderEx Header="{Binding Name}" IsExpanded="{Binding IsSelected, Mode=TwoWay}">
                                        <ItemsPresenter />
                                    </gem:ExpanderEx>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock FontWeight="Bold" FontSize="15"
                                   Text="{Binding Path=Name}"/>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListBox.GroupStyle>
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Margin="18 0 0 0" ToolTip="{Binding Help}">
                    <Image Source="{Binding IconSource, Converter={StaticResource NullableValueConverter}}" Margin="0 0 5 0" Width="16" />
                    <TextBlock Text="{Binding Name}" Padding="2" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

ViewModel.cs

items = new BindableCollection<ToolboxItemViewModel>();
        collectionView = CollectionViewSource.GetDefaultView(items);
        collectionView.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
        collectionView.GroupDescriptions.Add(new PropertyGroupDescription("SubCategory"));
        collectionView.Filter = ToolBoxFilter;

    private readonly BindableCollection<ToolboxItemViewModel> items;
    public IObservableCollection<ToolboxItemViewModel> Items { get { return items; } }

    private string searchTerm;
    public string SearchTerm
    {
        get { return searchTerm; }
        set
        {
            if (searchTerm == value)
                return;

            searchTerm = value;
            NotifyOfPropertyChange(() => SearchTerm);

            //TODO: implement a defer here (Rx Extensions might help)
            collectionView.Refresh(); //Refresh the filter.
        }
    }

ToolBoxItem.cs

public class ToolboxItemViewModel : PropertyChangedBase
{
    private readonly ToolboxItem model;

    public ToolboxItemViewModel(ToolboxItem model)
    {
        this.model = model;
        IsSelected = false;
    }

    public ToolboxItem Model
    {
        get { return model; }
    }

    public string Name
    {
        get { return model.Name; }
    }

    public string Category
    {
        get { return model.Category; }
    }

    public string SubCategory
    {
        get { return model.SubCategory; }
    }


    private bool isSelected;
    public bool IsSelected
    {
        get { return isSelected; }
        set
        {
            isSelected = value;
            NotifyOfPropertyChange(() => IsSelected);
        }
    }

过滤时如何展开分组项?

或者另一种方法,如果里面选择了任何ListboxItem,如何扩展组项。

编辑

试图弄清楚,结果我在gem:ExpanderEx IsExpanded="{Binding IsSelected}" 上遇到了绑定错误

BindingExpression 路径错误“对象”“CollectionViewGroupInternal”上找不到“IsSelected”属性

【问题讨论】:

    标签: c# wpf xaml mvvm listbox


    【解决方案1】:

    在您的collectionView 过滤器中,添加yourObject.IsSelected = true;

    当您在过滤器中返回 true 时,您可以更改 ToolboxItemViewModel 对象的属性。

    bool ToolBoxFilter(object item)
    {
        if (Search Criteria Match)
        {
            ToolboxItemViewModel model = (ToolboxItemViewModel)item;
            model.IsSelected = true;
            ... rest of your code
        }    
    }
    

    【讨论】:

    • 我发现了一个绑定错误gem:ExpanderEx IsExpanded="{Binding IsSelected}",也许(也许?)这是问题,但不知道如何解决.. 我已经编辑了我的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    相关资源
    最近更新 更多