Collapsed Items in Wrappanel still need place

淺唱寂寞╮ 提交于 2020-01-25 04:47:24

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!