现在,您必须双击或单击+图标。如果用户点击扩展的节点上的任何位置,是否有任何方法可以实现这一点?如何在单击项目上单击展开WPF TreeView
回答
也许不是最完美的解决方案,但这个工程:
static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
{
while (source != null && source.GetType() != typeof(T))
source = VisualTreeHelper.GetParent(source);
return source;
}
然后在TreeViewItem.Selected处理程序:
private void Treeview_Selected(object sender, RoutedEventArgs e)
{
var treeViewItem = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
if (treeViewItem != null) treeViewItem.IsExpanded = true;
}
的VisualUpwardSearch魔术就是从这里取:Select TreeView Node on right click before displaying ContextMenu
问候
我有这个相同的问题和f由于another StackOverflow post,我们找到了一个很好的解决方案。
在control.xaml的TreeView的元素,你可以直接挂接到TreeViewItem的Selected事件:
<TreeView ItemsSource="{StaticResource Array}" TreeViewItem.Selected="TreeViewItem_Selected"/>
然后在你的control.xaml.cs后面的代码,你可以从RoutedEventArgs抢选定树型视图和将其设置为IsExpanded:
private void TreeViewItem_Selected(object sender, RoutedEventArgs e)
{
TreeViewItem tvi = e.OriginalSource as TreeViewItem;
if (tvi == null || e.Handled) return;
tvi.IsExpanded = !tvi.IsExpanded;
e.Handled = true;
}
干净整洁。希望能帮助别人!
对于XAML代码+1。完美解决了这个问题。 (所接受的答案(我也赞同))。 – 2012-02-16 09:18:57
在我看来,这是最好的方法!只有一个缺陷:当您尝试通过单击折叠图标折叠未选择的项目时,它将被选中,并且事件处理程序会阻止折叠它。在切换“IsExpanded”属性后,你必须在'if(!e.Handled){...}'中封装事件处理程序并设置'e.Handled = true;'。 – kroimon 2012-09-13 21:41:46
此问题的另一个问题是,当您单击已选树视图时,它不会折叠或展开。 – 2016-04-21 14:10:22
当您使用键盘进行导航时,接受的解决方案具有奇怪的行为,并且当它已被选中时不会折叠该项目。或者,只需从TreeViewItem派生一个新类并重写MouseLeftButtonDown方法。您还需要将您的TreeView.ItemsSource设置为您的新TreeViewItem类的集合。
protected override void OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
{
if (!e.Handled && base.IsEnabled)
{
this.IsExpanded = !this.IsExpanded;
e.Handled = true;
}
base.OnMouseLeftButtonDown(e);
}
我有同样的问题,我用Style的功能,所以你不需要处理事件。
我定义了一个样式为树型视图
<Style x:Key="{x:Type TreeViewItem}" TargetType="{x:Type TreeViewItem}">
<!--<Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/>-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeViewItem}">
<CheckBox Style="{StaticResource TreeViewItemCB}" IsChecked="{Binding Path=IsExpanded,Mode=OneWayToSource,RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press">
<Grid Background="{StaticResource TreeViewItemBackground}" Margin="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border Name="Bd">
<ContentPresenter x:Name="PART_Header" ContentSource="Header"/>
</Border>
<ItemsPresenter x:Name="ItemsHost" Grid.Row="1"/>
</Grid>
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
的重要组成部分,是定义在ControlTemplate中的复选框,并与之结合。选中CheckBox后,只需单击一次即可展开该项目。
<CheckBox Style="{StaticResource TreeViewItemCB}" IsChecked="{Binding Path=IsExpanded,Mode=OneWayToSource,RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press">
这是checkBox的样式,因此它可以延伸并且不会显示带有笔触的框。
<Style x:Key="TreeViewItemCB" TargetType="CheckBox" BasedOn="{StaticResource baseStyle}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
<Setter Property="Background" Value="Transparent"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="CheckBox">
<ContentPresenter VerticalAlignment="Stretch" HorizontalAlignment="Stretch" RecognizesAccessKey="True"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
辉煌的解决方案! – l33t 2015-07-19 21:15:52
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Cursor" Value="Hand" />
<EventSetter Event="MouseUp" Handler="TreeViewItem_Click"/>
</Style>
</TreeView.ItemContainerStyle>
private void TreeViewItem_Click(object sender, MouseButtonEventArgs e)
{
((TreeViewItem) sender).IsExpanded = !((TreeViewItem) sender).IsExpanded;
Thread.Sleep(700);
}
这里是答案,享受它
答:阿里Rahimy
另一种计算策略是使用附加propperties。
public class VirtualOneClickExpandButtonBehavior : DependencyObject
{
public static bool GetEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(EnabledProperty);
}
public static void SetEnabled(DependencyObject obj, bool value)
{
obj.SetValue(EnabledProperty, value);
}
public static readonly DependencyProperty EnabledProperty =
DependencyProperty.RegisterAttached("Enabled", typeof(bool), typeof(VirtualOneClickExpandButtonBehavior),
new UIPropertyMetadata(false, EnabledPropertyChangedCallback
));
private static void EnabledPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var treeView = dependencyObject as TreeView;
if (treeView == null) return;
treeView.MouseUp += TreeView_MouseUp;
}
private static void TreeView_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var treeViewItem = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
if (treeViewItem != null) treeViewItem.IsExpanded = !treeViewItem.IsExpanded;
}
static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
{
while (source != null && source.GetType() != typeof(T))
source = VisualTreeHelper.GetParent(source);
return source;
}
}
然后你可以像这样使用它。
<TreeView controls:VirtualOneClickExpandButtonBehavior.Enabled="true" ItemsSource="{Binding HierarchicalModel}"/>
如果您使用MVVM模式,因为您不需要隐藏代码,这是一个不错的选择。
而且他对Markust的VisualUpwardSearch(DependencyObject源)
- 1. WPF展开TreeView单击鼠标
- 2. 单击TreeView项目打开窗口?
- 3. 如何通过单击节标题(Silverlight)来展开treeview节点
- 4. WPF Combobox允许单击外部项目
- 5. 单击项目ArrayAdapter
- 6. C#EXT:GridCommand:单击单击展开双击折叠RowExpander
- 7. 如何在Android中单击ListView项目
- 8. 单击时展开UITextView?
- 9. 点击展开菜单
- 10. 单击Recyclerview上的文字以展开
- 11. 双击QTreeView展开项目箭头
- 12. 重新展开treeview项目
- 13. 如何打开wpf treeview中的点击节点上的文件
- 14. WPF菜单 - 中级菜单项点击
- 15. 在Android上,单击以展开列表 - 并单击一个按钮?
- 16. 如何在WPF上下文中引用右键单击的对象菜单项单击事件处理程序?
- 17. 如何在WPF的ListBox中的某个项目上捕获鼠标单击?
- 18. 打开对话框正好在上下文菜单上单击项目
- 19. 如何在WPF中右键单击按钮上下文菜单?
- 20. wpf上下文菜单左键单击
- 21. 单击SlidingMenu上的项目时返回。
- 22. 事件单击一个项目上android.support.design.widget.NavigationView
- 23. 如何将列表绑定到单击列表项目时展开的布局
- 24. 在WPF TreeView中展开树视图项目
- 25. RecyclerView单击项目和SearchView
- 26. RecyclerView项目单击过渡
- 27. RibbonGallery项目命令单击
- 28. 如何右键单击列表框中的项目并打开WPF上的菜单
- 29. 单击Tkinter Treeview小部件的项目的命令?
- 30. 如何使用TargetType在WPF中单击?
颠覆了一个非常古老的答案。我正在尝试这个解决方案,但是我有一个带有AccessText的标签,并且我得到了System.Windows.Documents.Run元素作为OriginalSource。这不是一个visual或visual3D元素,因此GetParent会引发异常。任何想法如何去做呢? – 2015-10-09 12:25:34