[英]Changing ListBox ItemsPanelTemplate has gotten me into trouble?
I have a CustomControl
(say CC
) that has been inherited from ContentControl
and contains a ScrollViewer
which includes a ContentPresenter
. 我有一个
CustomControl
(例如CC
),它已从ContentControl
继承并包含一个ScrollViewer
,其中包括一个ContentPresenter
。 When I put a ListBox
into the CC
it works without any problem. 当我将一个
ListBox
放入CC
它可以正常工作。 But when I set the ItemsPanelTemplate
of the ListBox
it doesn't notify CC
to scroll into the ListBox
selected item. 但是,当我设置
ListBox
的ItemsPanelTemplate
时,它不会通知CC
滚动到ListBox
选定的项目中。
What's the reason for it? 是什么原因呢? -Thanks
-谢谢
UPDATE: 更新:
I'll encounter the problem described above only if I set HorizontalScrollBarVisibility
or VerticalScrollBarVisibility
to Hidden
and customize the ItemsPanelTemplate
of the ListBox
simultaneously. 仅当将
HorizontalScrollBarVisibility
或VerticalScrollBarVisibility
设置为Hidden
并同时自定义ListBox
的ItemsPanelTemplate
,我才会遇到上述问题。 (I need to hide scollbars.) (我需要隐藏滚动条。)
I wonder if hiding Scrollbars
prevents ScrollViewer
contents from notifying it to bring selected item into view, why this issue doesn't happen when I don't change items panel??? 我想知道是否隐藏
Scrollbars
阻止ScrollViewer
内容通知它以将选定的项目显示在视图中,为什么当我不更改项目面板时也不会发生此问题???
Generic.xaml: Generic.xaml:
<ResourceDictionary ...>
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Border ...>
<ScrollViewer ...
CanContentScroll="True"
HorizontalScrollBarVisibility="Hidden" « PROBLEM
VerticalScrollBarVisibility="Hidden"> «
<ContentPresenter Content="{TemplateBinding Content}"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
MainWindow.xaml: MainWindow.xaml:
<Window x:Class="MyNamespace1.MainWindow"
...
xmlns:proj="clr-namespace:MyNamespace0;assembly=...">
<Grid>
<proj:CustomControl1 x:Name="CC">
<ListBox>
<ListBox.ItemsPanel> «
<ItemsPanelTemplate> «
<StackPanel Orientation="Horizontal"/> « PROBLEM
</ItemsPanelTemplate> «
</ListBox.ItemsPanel> «
<!--content goes here-->
</ListBox>
</proj:CustomControl1>
</Grid>
</Window>
Have you set the IsItemsHost property for the Panel in the ItemsPanelTemplate to True? 您是否已将ItemsPanelTemplate中面板的IsItemsHost属性设置为True?
Eg if the itemspaneltemplate should use a Canvas: 例如,如果itemspaneltemplate应该使用Canvas:
<ItemsPanelTemplate>
<Canvas IsItemsHost="True" />
</ItemsPanelTemplate>
StackPanel treats its content has having infinite space.. You will have to limit its size explicitly, or change it to other panel, Grid for example. StackPanel对待其内容具有无限的空间。您将必须明确限制其大小,或将其更改为其他面板,例如Grid。
Try this: 尝试这个:
<ItemsPanelTemplate>
<Grid/>
</ItemsPanelTemplate>
Or: 要么:
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" Width="100" Height="100"/>
</ItemsPanelTemplate>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.