【问题标题】:wpf hide an element based on children count of another element in xamlwpf根据xaml中另一个元素的子项计数隐藏一个元素
【发布时间】:2018-09-26 13:21:59
【问题描述】:

纯粹使用 XAML,我想隐藏一个元素,如果 liststackpanel 有元素,则说 textblockimage

例如看下面的代码

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

我可以这样做是 cs,但我想知道一种仅在 XAML 中执行此操作的方法,以尽力确保 cs 仅具有应用程序逻辑。

编辑

我正在以编程方式向堆栈面板添加元素。

【问题讨论】:

    标签: c# wpf windows xaml


    【解决方案1】:

    为此,您可以在 Style 中使用 DataTrigger

    这是我们值得关注的StackPanel

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

    这是要隐藏的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>
    

    【讨论】:

    • 这不起作用。为了清楚起见,我正在以编程方式将项目添加到堆栈面板。
    • 你没有在你的问题中提到这一点,所以我认为它是静态 XAML。
    • 对不起,我应该有。更新问题
    猜你喜欢
    • 1970-01-01
    • 2022-07-15
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多