使“可观察集合”项不可见



这是我的Model

ObservableCollection<Student> StudentsList;
class Student
{
    static ObservableCollection<Student> Students;
    string SName;
    String FName;
}

我有一个简单的Combobox

<ComboBox
    x:Name="cbStudents" 
    ItemsSource="{Binding StudentsList}">

我想从我的Combobox项目/项目中隐藏,是否可以在不使用Students ObservableCollection Remove功能的情况下做到这一点?

编辑:

尝试mm8解决方案后,似乎我的combo box风格导致了我的错误,所以这是我的风格:

<!-- region combobox style -->
        <SolidColorBrush x:Key="ComboBoxNormalBorderBrush" Color="Transparent" />
        <SolidColorBrush x:Key="ComboBoxNormalBackgroundBrush" Color="#FF103766" />
        <SolidColorBrush x:Key="ComboBoxDisabledForegroundBrush" Color="#888" />
        <SolidColorBrush x:Key="ComboBoxDisabledBackgroundBrush" Color="#FF7AA0CD" />
        <SolidColorBrush x:Key="ComboBoxDisabledBorderBrush" Color="#FF7AA0CD" />
        <Style TargetType="{x:Type ComboBoxItem}">
            <Setter Property="SnapsToDevicePixels" Value="true" />
            <Setter Property="HorizontalAlignment" Value="Stretch" />
            <Setter Property="VerticalAlignment" Value="Stretch" />
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBoxItem">
                        <Border
                            Name="Border"
                            Padding="5"
                            Margin="0"
                            BorderThickness="0"
                            CornerRadius="0"
                            Background="Transparent"
                            BorderBrush="Transparent" >
                            <TextBlock TextAlignment="Left">
                                <ContentPresenter />
                            </TextBlock>
                        </Border>
                        <ControlTemplate.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="true"/>
                                    <Condition Property="IsSelected" Value="false"/>
                                    <Condition Property="IsHighlighted" Value="true"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Foreground" Value="White"/>
                                <Setter TargetName="Border" Property="BorderBrush" Value="#FF103766"/>
                                <Setter TargetName="Border" Property="Background" Value="#FF103766"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="false"/>
                                    <Condition Property="IsSelected" Value="true"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Foreground" Value="White"/>
                                <Setter TargetName="Border" Property="BorderBrush" Value="#FF103766"/>
                                <Setter TargetName="Border" Property="Background" Value="#FF103766"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="true"/>
                                    <Condition Property="IsSelected" Value="true"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Foreground" Value="White"/>
                                <Setter TargetName="Border" Property="BorderBrush" Value="#FF103766"/>
                                <Setter TargetName="Border" Property="Background" Value="#FF103766"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="false"/>
                                    <Condition Property="IsSelected" Value="false"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Foreground" Value="#FF103766"/>
                                <Setter TargetName="Border" Property="BorderBrush" Value="White"/>
                                <Setter TargetName="Border" Property="Background" Value="White"/>
                            </MultiTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <ControlTemplate TargetType="ToggleButton" x:Key="ComboBoxToggleButtonTemplate">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition Width="20" />
                </Grid.ColumnDefinitions>
                <Border 
                    Grid.ColumnSpan="2"
                    Name="Border"
                    BorderBrush="{StaticResource ComboBoxNormalBorderBrush}" 
                    CornerRadius="0"
                    BorderThickness="1, 1, 1, 1" 
                    Background="{StaticResource ComboBoxNormalBackgroundBrush}" />
                <Border 
                    Grid.Column="1"
                    Margin="1, 1, 1, 1"
                    BorderBrush="#444"
                    Name="ButtonBorder"
                    CornerRadius="0, 0, 0, 0"
                    BorderThickness="0, 0, 0, 0" 
                    Background="{StaticResource ComboBoxNormalBackgroundBrush}" />
                <Path
                    Name="Arrow"
                    Grid.Column="1" 
                    Data="M 0 0 L 8 12 L 16 0 Z"
                    HorizontalAlignment="Center"
                    Fill="#444"
                    VerticalAlignment="Center" 
                    Margin="-10,0,0,0"/>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="UIElement.IsMouseOver" Value="True">
                    <Setter Property="Panel.Background" TargetName="ButtonBorder" Value="Transparent"/>
                    <Setter Property="Shape.Fill" TargetName="Arrow" Value="White"/>
                    <Setter Property="Panel.Background" TargetName="Border" Value="#FF7AA0CD"/>
                </Trigger>
                <Trigger Property="UIElement.IsMouseOver" Value="False">
                    <Setter Property="Shape.Fill" TargetName="Arrow" Value="Transparent"/>
                </Trigger>
                <Trigger Property="ToggleButton.IsChecked" Value="True">
                    <Setter Property="Panel.Background" TargetName="ButtonBorder" Value="Transparent"/>
                    <Setter Property="Shape.Fill" TargetName="Arrow" Value="White"/>
                </Trigger>
                <Trigger Property="UIElement.IsEnabled" Value="False">
                    <Setter Property="Panel.Background" TargetName="Border" Value="{StaticResource ComboBoxDisabledBackgroundBrush}"/>
                    <Setter Property="Panel.Background" TargetName="ButtonBorder" Value="{StaticResource ComboBoxDisabledBackgroundBrush}"/>
                    <Setter Property="Border.BorderBrush" TargetName="ButtonBorder" Value="{StaticResource ComboBoxDisabledBorderBrush}"/>
                    <Setter Property="TextElement.Foreground" Value="{StaticResource ComboBoxDisabledForegroundBrush}"/>
                    <Setter Property="Shape.Fill" TargetName="Arrow" Value="Transparent"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
        <Style x:Key="ComboBoxFlatStyle"  TargetType="{x:Type ComboBox}">
            <Setter Property="UIElement.SnapsToDevicePixels" Value="True"/>
            <Setter Property="FrameworkElement.OverridesDefaultStyle" Value="True"/>
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
            <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
            <Setter Property="TextElement.Foreground" Value="Gainsboro"/>
            <Setter Property="FrameworkElement.FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBox">
                        <Grid>
                            <ToggleButton 
                                Name="ToggleButton"
                                Grid.Column="2"
                                ClickMode="Press"
                                Focusable="False"
                                IsChecked="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                                Template="{StaticResource ComboBoxToggleButtonTemplate}"/>
                            <ContentPresenter 
                                Name="ContentSite"
                                Margin="5, 4, 23, 3"
                                IsHitTestVisible="False"
                                HorizontalAlignment="Left"
                                VerticalAlignment="Center"                              
                                Content="{TemplateBinding ComboBox.SelectionBoxItem}" 
                                ContentTemplate="{TemplateBinding ComboBox.SelectionBoxItemTemplate}"
                                ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"/>
                            <TextBox
                                Name="PART_EditableTextBox"
                                Margin="3, 3, 23, 3"                     
                                IsReadOnly="{TemplateBinding IsReadOnly}"
                                Visibility="Hidden"
                                Background="Transparent"
                                HorizontalAlignment="Left"
                                VerticalAlignment="Center"
                                Focusable="True" >
                                <TextBox.Template>
                                    <ControlTemplate TargetType="TextBox" >
                                        <Border Name="PART_ContentHost" Focusable="False" />
                                    </ControlTemplate>
                                </TextBox.Template>
                            </TextBox>
                            <Popup 
                                Name="Popup"
                                Placement="Bottom"
                                Focusable="False"
                                AllowsTransparency="True"
                                IsOpen="{TemplateBinding ComboBox.IsDropDownOpen}"
                                PopupAnimation="Fade">
                                <Grid 
                                    Name="DropDown"
                                    SnapsToDevicePixels="True"
                                    MinWidth="{TemplateBinding FrameworkElement.ActualWidth}"
                                    MaxHeight="{TemplateBinding ComboBox.MaxDropDownHeight}">
                                    <Border
                                        Name="DropDownBorder"
                                        Background="Gainsboro"
                                        Margin="0, 0, 0, 0"
                                        CornerRadius="0"                                        
                                        BorderBrush="#FF103766"
                                        BorderThickness="1" />
                                    <ScrollViewer Margin="4" SnapsToDevicePixels="True">
                                        <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained" />
                                    </ScrollViewer>
                                </Grid>
                            </Popup>
                            <!-- Popup showing items -->
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="UIElement.IsMouseOver" Value="True">
                                <Setter Property="TextElement.Foreground" Value="White"/>
                            </Trigger>
                            <Trigger Property="UIElement.IsMouseOver" Value="False">
                                <Setter Property="TextElement.Foreground" Value="Gainsboro"/>
                            </Trigger>
                            <Trigger Property="ItemsControl.HasItems" Value="False">
                                <Setter Property="FrameworkElement.MinHeight" TargetName="DropDownBorder" Value="95"/>
                            </Trigger>
                            <Trigger Property="UIElement.IsEnabled" Value="False">
                                <Setter Property="TextElement.Foreground" Value="Gainsboro"/>
                            </Trigger>
                            <Trigger Property="ItemsControl.IsGrouping" Value="True">
                                <Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
                            </Trigger>
                            <Trigger Property="ComboBox.IsEditable" Value="True">
                                <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
                                <Setter Property="UIElement.Visibility" TargetName="PART_EditableTextBox" Value="Visible"/>
                                <Setter Property="UIElement.Visibility" TargetName="ContentSite" Value="Hidden"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <!-- endregion of Flat ComboBox -->

您可以使用带有DataTriggerItemContainerStyle来隐藏具有特定名称的Student,例如:

<ComboBox
            x:Name="cbStudents" 
            ItemsSource="{Binding StudentsList}">
    <ComboBox.ItemContainerStyle>
        <Style TargetType="ComboBoxItem">
            <Style.Triggers>
                <DataTrigger Binding="{Binding SName}" Value="SomeName">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>

编辑:(见下面的评论("ComboBoxFlatStyle"是一种ComboBox风格。ItemContainerStyle应基于隐式ComboBoxItem样式:

<ComboBox Style="{StaticResource ComboBoxFlatStyle}" ...>
    <ComboBox.ItemContainerStyle>
        <Style TargetType="ComboBoxItem" BasedOn="{StaticResource {x:Type ComboBoxItem}}">
            ...
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>

您需要 CollectionViewSource 和筛选器委托

#region feed items
    private ObservableCollection<OpdsItemBase> _FeedItems = new ObservableCollection<OpdsItemBase>();
    public ICollectionView FeedItemView
    {
        get
        {
            return CollectionViewSource.GetDefaultView(_FeedItems);
        }
    }
    #endregion

public string FilterText
    {
        get { return _filterText; }
        set
        {
            _filterText = value;
            FeedItemView.Filter = delegate(object obj)
            {
                if (string.IsNullOrEmpty(_filterText))
                    return true;
                OpdsItemBase data = obj as OpdsItemBase;
                if (data == null)
                    return false;
                return (data.Title.IndexOf(_filterText, 0, StringComparison.InvariantCultureIgnoreCase) > -1);
            };
        }
    }

相关内容

  • 没有找到相关文章

最新更新