[英]WPF QuickConverter: Hide element on empty list
我有一個StackPanel
,其中包含帶有標題的TextBox
和具有項目的ItemsControl
。 如果提供項目的列表為空,我想隱藏整個StackPanel。 我沒有嘗試為綁定編寫專用的Converter,而是想嘗試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>
第一個文本塊顯示預期的結果,所有其他QuickConverter表達式均無法正常工作。 在設計時和運行時都沒有錯誤或異常。
謝謝您的任何想法。
克里斯。
您可能會使用DataTrigger
Style
的DataTrigger
:
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.