我的TreeView
和DataGrid
存在问题。问题在于,每当我点击TreeView
(或DataGrid
)中的某个项目时,视图会向右水平滚动,我猜是为了使其更加明显。我有一些我TreeView
下面的图片:
在项目点击之前:
物品点击后
我确定我可以禁用水平滚动条,但我仍然希望用户可以选择滚动,而不是在单击某个项目时自动滚动。
非常感谢任何帮助,只是为了完整起见,我附上了我的TreeView代码:
<TreeView x:Name="TreeView"
ItemsSource="{Binding Path=FileStubsView}"
SelectedItemChanged="TreeView_OnSelectedItemChanged"
common:MultipleSelectTreeView.PreviewMouseDoubleClick="HandleTreeItemDoubleClick"
Tag="{Binding DataContext,RelativeSource={RelativeSource Mode=Self}}"
Grid.Row="1"
Grid.Column="0"
Margin="0,2,0,0"
TreeViewItem.Expanded="TreeViewItem_Expanded"
TreeViewItem.Collapsed="TreeViewItem_Collapsed" >
答案 0 :(得分:1)
以下是一个有效的解决方案,源自此answer中的question:
public partial class MainWindow : Window
{
public List<Item> Items
{
get
{
return new List<Item>()
{
new Item(),
new Item(),
new Item(),
new Item(),
};
}
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private void TreeViewItem_CancelRequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
e.Handled = true;
}
}
public class Item
{
public string Text
{
get
{
return "Itemaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
}
}
public List<Item> Items
{
get
{
return new List<Item>()
{
new Item(),
new Item(),
new Item(),
};
}
}
}
<Grid Width="200">
<TreeView ItemsSource="{Binding Items}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Text}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<EventSetter Event="Control.RequestBringIntoView"
Handler="TreeViewItem_CancelRequestBringIntoView" />
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
</Grid>