更改 ListBox ItemsPanelTemplate 给我带来了麻烦?
我有一个 CustomControl
(例如 CC
),它继承自 ContentControl
并包含一个 ScrollViewer
,其中包含一个 <代码>ContentPresenter。当我将 ListBox
放入 CC
时,它可以正常工作。但是,当我设置 ListBox
的 ItemsPanelTemplate
时,它不会通知 CC
滚动到 ListBox
所选项目。
其原因何在? -感谢
更新:
仅当我将HorizontalScrollBarVisibility
或VerticalScrollBarVisibility
设置为Hidden
并且同时自定义ListBox
的ItemsPanelTemplate
。 (我需要隐藏滚动条。)
我想知道隐藏 Scrollbars
是否会阻止 ScrollViewer
内容通知它将所选项目带入视图,为什么当我不这样做时不会发生此问题不改变项目面板???
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:
<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>
I have a CustomControl
(say CC
) that has been inherited from ContentControl
and contains a ScrollViewer
which includes a ContentPresenter
. When I put a ListBox
into the CC
it works without any problem. But when I set the ItemsPanelTemplate
of the ListBox
it doesn't notify CC
to scroll into the ListBox
selected item.
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. (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???
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:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否将 ItemsPanelTemplate 中面板的 IsItemsHost 属性设置为 True?
例如,如果 itemspaneltemplate 应使用 Canvas:
相关
Have you set the IsItemsHost property for the Panel in the ItemsPanelTemplate to True?
E.g. if the itemspaneltemplate should use a Canvas:
Related
StackPanel 对待它的内容有无限的空间。
您必须明确限制其大小,或将其更改为其他面板,例如网格。
试试这个:
或者:
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.
Try this:
Or: