问题
I have a Wrappanel inside a TreeViewItem. The Items inside the Wrappanel can be made (in)visible by a filter. The problem is that the collapsed Items still need a small place what corrupts the alignment (all Items have a fixed width, Margin and Padding is 0).
How can I remove the superfluous space?
Part of the XAML (inside the TreeViewItem Style):
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<CheckBox IsChecked="{Binding Assigned, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ToolTip="{Binding Description}"
Click="CheckBox_Clicked"
FontFamily="Courier New"
Padding="0,0,0,0"
Margin="0,0,0,0">
<TextBlock Text="{Binding FixedLengthName}"/>
<CheckBox.Visibility>
<MultiBinding Converter="{StaticResource PermVisibilityConv}">
<Binding Path="IsChecked" ElementName="ChangesOnly"/>
<Binding Path="Changed"/>
<Binding Path="Visible"/>
</MultiBinding>
</CheckBox.Visibility>
</CheckBox>
</DataTemplate>
</Setter.Value>
</Setter>
without filter:
with filter:
回答1:
Thanks to the help of ZSH, I found a solution.
It's not enough to Collapse the CheckBox, the Container around it also must be collapsed.
After I moved the Visibility-Binding from the CheckBox to the ItemContainerStyle, the View behaved as it should.
the same part of the XAML as above, now without space occupied by collapsed Items:
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Visibility">
<Setter.Value>
<MultiBinding Converter="{StaticResource PermVisibilityConv}">
<Binding Path="IsChecked" ElementName="ChangesOnly"/>
<Binding Path="Changed"/>
<Binding Path="Visible"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<CheckBox IsChecked="{Binding Assigned, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ToolTip="{Binding Description}" Click="CheckBox_Clicked"
FontFamily="Courier New">
<TextBlock Text="{Binding FixedLengthName}"/>
</CheckBox>
</DataTemplate>
</Setter.Value>
</Setter>
来源:https://stackoverflow.com/questions/33341657/collapsed-items-in-wrappanel-still-need-place