C# 垂直滚动查看器移动列表框

C# 垂直滚动查看器移动列表框,c#,wpf,xaml,listbox,treeview,C#,Wpf,Xaml,Listbox,Treeview,上下文 我有一个树状视图,可根据我们的主题和需求定制。最后一级树层次结构是元素列表(下面有文本的缩略图)。它们排列在两列中,两个元素排成一行。这是使用带有UniformGrid的ListBox作为ItemsPanel实现的。我有一个用于整个TreeView的ScrollViewer。ListBox本身包含在倒数第二个treevieItem中,它具有重载样式(见下文)。这是必需的,这样我可以在每行放置两个元素,并删除扩展器和缩进等 问题 当TreeView中的项目数增加时,滚动条会出现,但它会将列

上下文

我有一个
树状视图
,可根据我们的主题和需求定制。最后一级树层次结构是元素列表(下面有文本的缩略图)。它们排列在两列中,两个元素排成一行。这是使用带有
UniformGrid
ListBox
作为
ItemsPanel
实现的。我有一个用于整个
TreeView
ScrollViewer。
ListBox
本身包含在倒数第二个
treevieItem
中,它具有重载样式(见下文)。这是必需的,这样我可以在每行放置两个元素,并删除
扩展器
和缩进等

问题

TreeView
中的项目数增加时,滚动条会出现,但它会将
列表框
移到左侧。如果我折叠某些元素,滚动条将消失,项目(带文本的缩略图)将再次向右移动

我尝试的

我已尝试在
ScrollViewer
TreeView
ContentPresenter
上指定
Width
MaxWidth
并禁用
HorizontalScrollBarVisibility
。但现在还是运气好。我不知道如何防止出现
ScrollViewer
时项目向左移动

列表框XAML

 <ListBox ItemsSource="{Binding Children}" Background="Black" SelectionMode="Extended" ScrollViewer.HorizontalScrollBarVisibility="Disabled" MaxWidth="230">
     <ListBox.ItemContainerStyle>
         <Style TargetType="ListBoxItem">
             <Setter Property="Padding" Value="0"></Setter>
             <Setter Property="HorizontalAlignment" Value="Center"></Setter>
             <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
             <Setter Property="Width" Value="95"></Setter>
             <Setter Property="IsSelected" Value="{Binding IsSelected,Mode=OneWayToSource}"></Setter>
             <Setter Property="Template">
                 <Setter.Value>
                     <ControlTemplate TargetType="ListBoxItem">
                         <ContentPresenter MaxWidth="230" Width="230" ScrollViewer.HorizontalScrollBarVisibility="Disabled"></ContentPresenter>
                     </ControlTemplate>
                 </Setter.Value>
             </Setter>
         </Style>
     </ListBox.ItemContainerStyle>
     <ListBox.ItemsPanel>
         <ItemsPanelTemplate>
             <UniformGrid Columns="2" Width="220" Margin="0,15,0,0"></UniformGrid>
         </ItemsPanelTemplate>
     </ListBox.ItemsPanel>
 </ListBox>

倒数第二种树形文字样式

 <Style TargetType="TreeViewItem" x:Key="myStyle">
     <Setter Property="Template">
         <Setter.Value>
             <ControlTemplate TargetType="TreeViewItem">
                 <ContentPresenter ContentSource="Header" Margin="{Binding Converter={StaticResource mySNThumbnailItemHostMarginConverter}}" 
                                   MaxWidth="230" Width="230" ScrollViewer.HorizontalScrollBarVisibility="Disabled"></ContentPresenter>
             </ControlTemplate>
         </Setter.Value>
     </Setter>
 </Style>


我怎样才能做到这一点?谢谢你的帮助

您正在为ListBoxItems设置固定宽度,因此,当滚动条可见时,没有足够的空间容纳它,因此它会将内容推到左侧。考虑删除固定宽度。@ MikEason感谢回答。我试过了,但没用。还有其他建议吗?