使用ItemsControl组合框进行动态过滤

问题描述 投票:1回答:2

我遇到这种情况,我想显示一个管理对象列表和每个管理的ComboBox。在此ComboBox中,我想要一个列表,其中包含属于该主管部门的雇员,以及一个空选项。因此,我需要根据管理进行过滤。

到目前为止,我已经提出了以下代码(注意:对象名称已翻译)

<ItemsControl x:Name="listAdministrations" ItemsSource="{Binding Path=AllAdministrations}" Margin="0,0,0,6">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical" >
                <TextBox Content="{Binding Path=AdministrationName}" />

                <StackPanel Orientation="Horizontal" Margin="14,0,0,0">
                    <Label>Declares under:</Label>
                    <ComboBox DisplayMemberPath="DisplayFullName">
                        <ComboBox.ItemsSource>
                            <CompositeCollection>
                                <!-- empty option -->
                                <model:Employee DisplayFullName="-" />
                                <CollectionContainer Collection="{Binding Source={StaticResource employeeCV}}"/>
                            </CompositeCollection>
                        </ComboBox.ItemsSource>
                    </ComboBox>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

静态资源employeeCV是附加了过滤器事件的CollectionViewSource。但是我必须以某种方式将ItemsControl循环中的当前管理传递给此事件。在数据绑定中,这转换为{Binding Path=.}中的ItemsControl。发件人对象是我的CollectionViewSource,但这没有提供有用的数据。

类似这样的东西:

private void EmployeeAdministrationFilter( object sender, FilterEventArgs e )
    {
        Employee employee = ( Employee )e.Item;
        Administration administration; // how do I pass the administration to this filter?
}
c# .net wpf data-binding
2个回答
0
投票

我不知道该怎么做,但我可以建议另一种方法:为您的Administration类创建一个扩展方法。此方法创建一个过滤的集合视图并返回它。然后,您可以绑定到方法的结果。


0
投票

您可能想要尝试:http://dotnetexplorer.blog.com/2011/04/07/wpf-itemscontrol-generic-staticreal-time-filter-custom-control-presentation/

这是一个通用的,动态的,完整的XAML声明性项目控件过滤器用户控件。它可以过滤datagrid,listbox,combobox等...

希望这会有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.