简体   繁体   English

禁用列表框时隐藏列表框中的项目

[英]Hide items in a ListBox when ListBox is disabled

When a standard ListBox is disabled, the elements in it are simply grayed out. 禁用标准ListBox时,其中的元素仅显示为灰色。 They are still visible, despite not being clickable. 尽管无法单击,但它们仍然可见。 I would like to know if it is possible, and if it is, how to hide these items when the ListBox is disabled. 我想知道是否有可能,如果有可能,在禁用列表框时如何隐藏这些项目。 I do not want to remove the elements from the ItemSource or create a temp storage for them. 我不想从ItemSource中删除元素或为它们创建临时存储。

So far, I thought about changing the visibility of the ListBox but that gets rid of the entire thing (including the border lines). 到目前为止,我曾考虑过更改ListBox的可见性,但这摆脱了整个事情(包括边界线)。

You could bind Visibility of ListBoxItem to IsEnabled of ListBox 您可以将ListBoxItem Visibility绑定到ListBox IsEnabled

<ListBox ItemsSource="{Binding DataSource}">
    <ListBox.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    </ListBox.Resources>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Visibility" Value="{Binding IsEnabled, RelativeSource={RelativeSource AncestorType=ListBox}, Converter={StaticResource BooleanToVisibilityConverter}}" />
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

Just add another ListBox to the form and when you disable the first listbox with the elements inside of it, hide it, and then display the other to the user which will essentially be an empty greyed out listbox. 只需将另一个ListBox添加到窗体中,然后在禁用其中包含元素的第一个列表框时,将其隐藏,然后向用户显示另一个,这实际上是一个空的灰色列表框。

Then when you enable the box back, hide the empty one and show the real one. 然后,当您启用该框时,隐藏空白框并显示真实框。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM