ComboBoxItem不会崩溃

时间:2015-03-09 16:00:36

标签: wpf

我写了一个基本的可见性转换器,当属性“Active”为true时,ComboBoxItem应该是Visible,否则就会折叠。它当前正确显示活动的那些,不活动的文本是不可见的,但仍然可以看到该项目。

http://snag.gy/Mh2Xq.jpg

请问如何让ComboBoxItem正确地折叠无效的组合框架。

<ComboBox Grid.Row="1" Grid.Column="2" SelectedItem="{Binding Product, Mode=TwoWay}" ItemsSource="{Binding Products}" VerticalContentAlignment="Center">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}">
                    <TextBlock.Style>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="Visibility" Value="{Binding Active, Converter={StaticResource VisibilityConverter }}"></Setter>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
            </StackPanel>
        </DataTemplate>
   </ComboBox.ItemTemplate>
</ComboBox>

3 个答案:

答案 0 :(得分:1)

将可见性转换器应用于父堆栈面板。像这样:

<StackPanel Orientation="Horizontal" Visibility="{Binding Active, Converter={StaticResource VisibilityConverter}}">
...
</StackPanel>

答案 1 :(得分:1)

<ComboBox.Resources>
    <Style TargetType="ComboBoxItem">
        <Setter Property="Visibility"  Value="{Binding Active, Converter={StaticResource VisibilityConverter}}" />
    </Style>
</ComboBox.Resources>

答案 2 :(得分:0)

您应该根据IsActive

过滤绑定列表

尝试将comboBox绑定到

Products.Where(t => t.IsActive)