WPF QuickConverter:在空列表中隐藏元素

Chr*_*phe 5 c# wpf xaml

我有一个StackPanel保存一个TextBox带有标题和ItemsControl与项目。如果提供项目的列表为空,我想隐藏整个 StackPanel。我不想为绑定编写专用的转换器,而是想尝试一下QuickConverter ( https://quickconverter.codeplex.com/ )。QuickConverter 允许在绑定中使用内联 C# 表达式。

所以这是我的标记:

<StackPanel Visibility="{qc:Binding '$P > 0 ? Visibility.Visible : Visibility.Collapsed', P={Binding Path=Value.Count}}"> <!-- this does not work. It's always shown, regardless of the element count -->
  <TextBlock Text="{qc:Binding '$P', P={Binding Path=Value.Count}}"></TextBlock> <!-- for debugging purposes only. It correctly shows the element count for the list -->
  <TextBlock Text="{qc:Binding '$P.Count', P={Binding Path=Value}}"></TextBlock> <!-- for debugging purposes only. It should do the same as the line above, but it does nothing -->

  ...

  <ItemsControl ItemsSource="{Binding Path=Value}">
    ...
  </ItemsControl>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

第一个文本块显示预期结果,所有其他 QuickConverter 表达式都无法工作。在设计时和运行时都没有错误或异常。

谢谢你的任何想法。

克里斯。

Cle*_*ens 6

你可能有DataTrigger一个Style是这样的:

<StackPanel>
    <StackPanel.Style>
        <Style TargetType="StackPanel">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Value.Count}" Value="0">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Style>
    ...
</StackPanel>
Run Code Online (Sandbox Code Playgroud)