简体   繁体   English

wpf根据xaml中另一个元素的子元素计数隐藏元素

[英]wpf hide an element based on children count of another element in xaml

Using purely XAML , I want to hide an element, say a textblock or an image if a list or a stackpanel has elements. 使用纯XAML ,如果liststackpanel具有元素,我想隐藏一个元素,例如textblockimage

For example, see the following code 例如,请参见以下代码

<Label x:Name="LabelTobeHidden" 
       Content="No one has joined" 
       Visibility="Visible"
       />
<StackPanel x:Name="Players" Orientation="Vertical"/>

I can do this is cs, but I want to know of a way to do this solely in XAML to try my best to ensure that cs only has the application logic. 我可以通过CS来做到这一点,但是我想知道一种仅在XAML中做到这一点的方法,以尽最大努力确保CS仅具有应用程序逻辑。

Edit : 编辑

I am adding elements to the stackpanel programmatically. 我正在以编程方式将元素添加到堆栈面板。

You can use a DataTrigger in a Style for that. 您可以DataTrigger使用StyleDataTrigger

This is our StackPanel to watch for: 这是我们要注意的StackPanel

<StackPanel x:Name="StackPanelToWatch" Orientation="Horizontal">
  <Rectangle Width="50" Height="50" Fill="Red"/>
</StackPanel>

And here is the Label to hide: 这是要隐藏的Label

<Label Content="text">
  <Label.Style>
    <Style TargetType="Label">
      <Style.Triggers>
        <DataTrigger Binding="{Binding Children.Count, ElementName=StackPanelToWatch}" Value="0">
          <Setter Property="Visibility" Value="Collapsed"/>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Label.Style>
</Label>

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

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