我需要一个ItemsView的ItemsPanel模板依赖于属性IsClusterSupported的ItemsControl。 如果没有ScrollViewer,ItemsControl将始终显示正确的ItemsPanel,但是当我添加滚动查看器代码时,即使IsClusterSupported = False,也会显示ClusterMapItemsPanelTemplate。itemtemplate周围的滚动查看器捡起错误的itemstemplate
这里可能会发生什么问题?我附上了下面的代码。
<Grid Name="MapGrid">
<ScrollViewer>
<ScrollViewer.Style>
<Style TargetType="{x:Type ScrollViewer}">
<Setter Property="VerticalScrollBarVisibility" Value="Hidden"/>
<Setter Property="HorizontalScrollBarVisibility" Value="Hidden"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsScrollingSupported}" Value="True">
<Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="HorizontalScrollBarVisibility" Value="Auto"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ScrollViewer.Style>
<ItemsControl Name="ItemsControlInnerMap" Loaded="ItemsControlInnerMap_Loaded"
ItemsSource="{Binding ItemsCol}" Tag="{Binding IsClusterSupported, PresentationTraceSources.TraceLevel=High}">
<ItemsControl.Style>
<Style TargetType="{x:Type ItemsControl}">
<Setter Property="ItemsPanel" Value="{StaticResource MapItemsPanelTemplate}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsClusterSupported}" Value="True">
<Setter Property="ItemsPanel" Value="{StaticResource ClusterMapItemsPanelTemplate}"/>
<Setter Property="ItemContainerStyle" Value="{StaticResource ClusterMapGridContainerStyle}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ItemsControl.Style>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border>
<TextBlock Text="{Binding LabelTxt}"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.GroupStyle>
<GroupStyle x:Name="MyGroup">
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border Name="BD" BorderBrush="Black" BorderThickness="2">
<ItemsPresenter Name="ClusterPresenter" Tag="{Binding Name}"/>
</Border>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Name, Converter={StaticResource BorderVisibilityConvertor}}" Value="false">
<Setter TargetName="BD" Property="BorderBrush" Value="Transparent"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<!--http://stackoverflow.com/questions/17863271/wpf-using-grid-as-itemshost-stacking-multiple-items-in-single-cell-automatical-->
<Setter Property="Grid.Row" Value="{Binding Items[0].ClusterGridRow}"/>
<Setter Property="Grid.Column" Value="{Binding Items[0].ClusterGridColumn}"/>
<Setter Property="Margin" Value="5"/>
</Style>
</GroupStyle.ContainerStyle>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<Grid Name="ClusterMapGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Loaded="ClusterMapGrid_Loaded"
MaxHeight="{Binding AllowedMaxHeight}" MaxWidth="{Binding AllowedMaxWidth}"
MinHeight="{Binding ReqMinHeight}" MinWidth="{Binding ReqMinWidth}">
</Grid>ReqMinHeight
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</ItemsControl.GroupStyle>
</ItemsControl>
</ScrollViewer>
</Grid>
RDV(2016年1月4日):我发现这里一些有趣的事情,希望越来越接近解决方案,但仍然需要帮助。
我所有的绑定工作正常;我用&调试没有scrollviewer,并在ItemsControl中看到正确的ItemsPanel。 ItemsControl的IsGrouping属性也设置正确。
当我在ItemsControl附近有一个滚动查看器时,ClusterMapGrid(GroupStyle.Panel)会被加载而不管我有没有集群。一旦加载,非集群模板(MapsItemsPanelTemplate)不会被加载。
为了解决这个问题,我尝试将ClusterMapGrid可见性绑定到ItemsControl的IsGrouping属性,但是又一次以某种方式将ClusterMapGrid加载到scrollviewer的情况下,并且一旦加载,非集群模板加载失败。
我用snoop来检查ClusterMapGrid的可见性,我可以看到它已经崩溃了,但是如果手动将它改为snoop为任何其他值,非集群模板加载并且一切正常。我尝试将可见性设置为在ClusterMapGrid_Loaded事件中折叠,但它也没有帮助。
进一步的调试我发现ScrollViewer正在加载其中的所有项目 - 因此ClusterMapGrid被加载,一旦我使用Snoop更改可见性,在非群集情况下它将被卸载。
问题声明:如何在加载ClusterMapGrid后卸载它,我知道这是非集群方案。
将设置' “ItemsControl”上的DataContext显式工作? '' –
Kcvin
另外,请查看输出窗口中的任何绑定/应用程序错误并更新问题以告诉我们它们是什么如果有的话。 – Kcvin
我验证了(使用snoop并附加一个调试器),根据IsClusterSupported值正确设置ItemsControl的Tag属性。出于某种原因,PresentationTraceSources.TraceLevel = High不起作用。 – RDV