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

标签 wpf xaml combobox visibility

有没有办法在 WPF 中隐藏组合框的项目? 在我的用户控件中,有一个带有复选框项的 ListBox 绑定(bind)到一个 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>

我使用了 this solution管理组合框项目并添加属性“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; }
}

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

最佳答案

非常简单:只需根据 ComboBoxItemIsSelected 的值,使用触发器设置 ComboBoxItem.Visibility 的组合框项样式>的DataContext:

<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>

如果在网格中加载 ComboBox 后,您可能会更新任何这些项目的 IsSelected 的值,则需要 implement INotifyPropertyChangedCBItem 上,以便 UI 反射(reflect)更改。

关于wpf - 如何在 WPF 中隐藏组合框的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38099669/

相关文章:

wpf - 如何创建可重用的 WPF 网格布局

c# - WPF 依赖属性返回值

c# - WPF 中的实例绑定(bind)与 MVVM

c# - XAML:访问用户控件内的控件

c# - 将一个 child 附加到网格,设置它的行和列

vb.net - 使用相同的值填充多个组合框

c++ - 带有 JPEG 图像的组合框

wpf - 为什么 WPF Border 的 "Border"在设计模式下可见,但在应用程序中却看不到?

c# - UWP 网格过渡

C# Windows 窗体如何根据第一个组合框中的选择更改第二个组合框的值