Weried Issue When Setting Margin in ItemContainerStyle
Closed this issue · 3 comments
Describe your issue
A clear and concise description of what your issue is. If you think that it is a bug, please provide the steps to reproduce (e.g. a code snippet or screenshots).
I'm using the code display items:
<ListView
Background="Transparent"
BorderBrush="Transparent"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemTemplate="{StaticResource ProxyItemTemplate}"
SelectionChanged="ProxyListView_SelectionChanged"
SelectionMode="Single"
VirtualizingPanel.CacheLengthUnit="Page"
VirtualizingPanel.CacheLength="1,1"
VirtualizingPanel.ScrollUnit="Pixel"
VirtualizingPanel.VirtualizationMode="Standard"
VirtualizingPanel.IsVirtualizingWhenGrouping="True"
x:Name="ProxyListView">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Margin" Value="0,0,5,5"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemsPanel>
<ItemsPanelTemplate x:Name="ItemsPanelTemplate">
<wpftk:VirtualizingWrapPanel
SpacingMode="None"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.GroupStyle>
<GroupStyle HidesIfEmpty="True">
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="{Binding Name, Mode=OneWay, Converter={StaticResource GroupNameToExpandedConverter}}">
<Expander.Header>
<emoji:TextBlock
Margin="10,0,0,0"
HorizontalAlignment="Left"
FontSize="20"
Text="{Binding Name}"/>
</Expander.Header>
<ItemsPresenter/>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<VirtualizingStackPanel
Orientation="{Binding Orientation, Mode=OneWay}" />
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
If I try to set margin for items, a lot of xaml binding errors occured:
HorizontalContentAlignment ListViewItem.HorizontalContentAlignment HorizontalAlignment
Could not find Source: RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1'。
VerticalContentAlignment ListViewItem.VerticalContentAlignment VerticalAlignment
Could not find Source: RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1'。
Scroll to the buttom and scroll up a little bit. Keep repeating this process you will see that the xaml binding errors soon exceeds 99+.
Sometimes, the scroll bar will out of control and keep going up and down forever. You have to manually kill the application in process manager in order to stop it.
Here is a gif example that I record, you can see that the scrollbar will vibrate up and down without my intention.
Version Info
Package Version: 1.5.3
.NET Version: .NET Framework 4.6.1
OS Version: Windows 10 Build 18363.1379
I will have a look at it later this week.
This is a common WPF problem, as you can see for example here: https://stackoverflow.com/questions/160391/listbox-with-grid-as-itemspaneltemplate-produces-weird-binding-errors/
To solve the problem you can simply put this in your Application.xaml file.
<Application.Resources>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Top" />
</Style>
</Application.Resources>
Closing this issue now because of inactivity. If you still have a problem let me know.