隐藏内容控件

发布于 2024-07-26 09:01:44 字数 167 浏览 4 评论 0原文

下面是一幅精美的图稿,它代表了一个 WPF 表单,左侧有一个列表框,右侧有一个内容控件。 我想设置它,以便如果列表框为空,则内容控件不可见。 我应该挂钩什么属性/事件?

----- -----
| a | | c |
| b | |   |
----- -----

Below is a fine piece of artwork that represents a WPF form with a listbox on the left and a content control on the right. I would like to set it so if the list box is empty, then the content control is invisible. What property/event should I hook to?

----- -----
| a | | c |
| b | |   |
----- -----

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榕城若虚 2024-08-02 09:01:44

您应该为 ContentControl 创建一个样式,并使用触发器来确定列表何时有 0 个项目,如下所示:

<ListBox x:Name="uiList">...</ListBox>
<ContentControl>
        <ContentControl.Content>
            <TextBox Text="List has items." />
        </ContentControl.Content>
        <ContentControl.Style>
            <Style TargetType="{x:Type ContentControl}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=uiList, Path=Items.Count}"
                                 Value="0">
                        <Setter Property="Visibility"
                                Value="Collapsed" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ContentControl.Style>
    </ContentControl>

You should create a Style for the the ContentControl, and use a Trigger to determine when the List has 0 items, like so:

<ListBox x:Name="uiList">...</ListBox>
<ContentControl>
        <ContentControl.Content>
            <TextBox Text="List has items." />
        </ContentControl.Content>
        <ContentControl.Style>
            <Style TargetType="{x:Type ContentControl}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=uiList, Path=Items.Count}"
                                 Value="0">
                        <Setter Property="Visibility"
                                Value="Collapsed" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ContentControl.Style>
    </ContentControl>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文