如何在 WPF 中隐藏组合框的项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38099669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:51:37  来源:igfitidea点击:

How to hide items of a combobox in WPF

wpfxamlcomboboxvisibility

提问by Andreas Sawatzki

Is there a way to hide items of a combobox in WPF? In my usercontrol there is a ListBox with checkbox-items bound to an ObservableCollection and a datagrid with a combobox.

有没有办法在 WPF 中隐藏组合框的项目?在我的用户控件中,有一个 ListBox 带有绑定到 ObservableCollection 的复选框项和一个带有组合框的数据网格。

<ListBox x:Name="AvailableAttributes" Grid.Row="0" Grid.Column="2" SelectionMode="Single" >
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=OneWay}"/>
        </Style>
    </ListBox.ItemContainerStyle>

    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding Name}" IsChecked="{Binding IsSelected}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

... 

<DataGrid Name="datagrid" AutoGenerateColumns="False" >
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Name}" />
            <DataGridComboBoxColumn 
                SelectedValueBinding="{Binding CBID}" 
                DisplayMemberPath="Name" 
                SelectedValuePath="ID">

                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="{x:Type ComboBox}">
                        <Setter Property="ItemsSource" 
                            Value="{Binding Path=CBItems, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="{x:Type ComboBox}">
                        <Setter Property="ItemsSource" 
                            Value="{Binding Path=CBItems, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>
        </DataGrid.Columns>
    </DataGrid>

I used this solutionto manage the combobox items and added the property 'IsSelected'

我使用此解决方案来管理组合框项目并添加了属性“IsSelected”

public class GridItem
{
    public string Name { get; set; }
    public int CBID { get; set; }
}

public class CBItem
{
    public int ID { get; set; }
    public string Name { get; set; }
    public bool IsSelected { get; set; }
}

Now I want to use the 'IsSelected' property to hide/show the item in the combobox. Can someone tell me how can I achieve this?

现在我想使用“IsSelected”属性来隐藏/显示组合框中的项目。有人可以告诉我如何实现这一目标吗?

回答by 15ee8f99-57ff-4f92-890c-b56153

Pretty simple: Just give the combobox items a style with a trigger that sets ComboBoxItem.Visibilityaccording to the value of IsSelectedon the ComboBoxItem's DataContext:

非常简单:只需为组合框项目设置一个带有触发器的样式,该触发器ComboBoxItem.Visibility根据'sIsSelected上的值进行设置:ComboBoxItemDataContext

<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
    <Setter Property="ItemsSource" 
        Value="{Binding Path=CBItems, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="ComboBoxItem" BasedOn="{StaticResource {x:Type ComboBoxItem}}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsSelected}" Value="False">
                        <Setter Property="Visibility" Value="Collapsed" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

If you might ever be updating the value of IsSelectedon any of these items after the ComboBoxes are loaded in the grid, you will need to implement INotifyPropertyChangedon CBItemso that the UI will reflect the change.

如果您可能IsSelected在 ComboBox 加载到网格中之后更新任何这些项目的值,您将需要实现 INotifyPropertyChangedonCBItem以便 UI 将反映更改。