如何判断treeview的叶子节点(当使用HierarchicalDataTemplate)

tengfeipang 2011-07-12 02:32:14
<HierarchicalDataTemplate DataType="{x:Type model:DirectoryTree}" ItemsSource="{Binding Path=ChildNodes}">
<StackPanel Orientation="Horizontal" Margin="0,2,0,2">
<Label local:AttachedPropertyHelper.MouseDoubleClick="{Binding Path=MouseDoubleClickCommand}" Content="{Binding Name}">
</Label>
</StackPanel>
</HierarchicalDataTemplate>

想问下大家,我Label里有个附加双击属性,但我只需要它为叶子节点时,事件才被触发,要怎样判断?
...全文
160 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
tengfeipang 2011-07-13
  • 打赏
  • 举报
回复
恩,附加两个属性也能实现,不过感觉不是很爽
tengfeipang 2011-07-13
  • 打赏
  • 举报
回复
茅塞顿开,呵呵,谢谢duanzilin的解答,记得上个问题也是你帮我解答的,本人刚学wpf,以后还要多多请教。结贴了
沝林 2011-07-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 tengfeipang 的回复:]
我附加属性最后这个写

public class AttachedPropertyHelper
{
public static DependencyProperty IsLeafProperty = DependencyProperty.RegisterAttached(
"IsLeaf",
typeof(bool),
typeof……
[/Quote]

这么写应该要进行两个附加属性定义,不知道是否实现了你要的功能?
沝林 2011-07-13
  • 打赏
  • 举报
回复
local:AttachedPropertyHelper.MouseDoubleClick="{Binding Path=MouseDoubleClickCommand}"

你看这一句,其实他是将MouseDoubleClick绑定到ICommand类型的变量,你可以将它绑定到自定义类型变量,让它同时包含Command和节点类型,这样在AttachedPropertyHelper里可以对Command的CanExecute进行处理
tengfeipang 2011-07-13
  • 打赏
  • 举报
回复
我附加属性最后这个写

public class AttachedPropertyHelper
{
public static DependencyProperty IsLeafProperty = DependencyProperty.RegisterAttached(
"IsLeaf",
typeof(bool),
typeof(AttachedPropertyHelper),
new FrameworkPropertyMetadata(null)
);
public static void SetIsLeaf(DependencyObject obj, bool value)
{
obj.SetValue(AttachedPropertyHelper.IsLeafProperty, value);
}
public static bool GetIsLeaf(DependencyObject obj)
{
return (bool)obj.GetValue(AttachedPropertyHelper.IsLeafProperty);
}

public static DependencyProperty MouseDoubleClickProperty = DependencyProperty.RegisterAttached(
"MouseDoubleClick",
typeof(ICommand),
typeof(AttachedPropertyHelper),
new FrameworkPropertyMetadata(null, new PropertyChangedCallback(MouseDoubleChanged))
);
public static void SetMouseDoubleClick(DependencyObject obj, ICommand value)
{
obj.SetValue(AttachedPropertyHelper.MouseDoubleClickProperty, value);
}
public static ICommand GetMouseDoubleClick(DependencyObject obj)
{
return (ICommand)obj.GetValue(AttachedPropertyHelper.MouseDoubleClickProperty);
}
public static void MouseDoubleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Control control = d as ContentControl;
if (control != null)
{
if (e.NewValue != null && e.OldValue == null)
{
control.MouseDoubleClick += new MouseButtonEventHandler(control_MouseDoubleClick);
}
else if (e.NewValue == null && e.OldValue != null)
{
control.MouseDoubleClick -= new MouseButtonEventHandler(control_MouseDoubleClick);
}
}
}
public static void control_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
Control control = sender as Control;
ICommand command = (ICommand)control.GetValue(AttachedPropertyHelper.MouseDoubleClickProperty);
if (command.CanExecute(control.GetValue(AttachedPropertyHelper.IsLeafProperty)))
{
command.Execute(control);
}
}
}
tengfeipang 2011-07-13
  • 打赏
  • 举报
回复
兵丁=绑定,打错,呵呵
tengfeipang 2011-07-13
  • 打赏
  • 举报
回复
感谢duanzilin,不过“修改附加属性的定义,让它可以接受参数,你可以将IsLeaf传给MouseDoubleClickCommand”这句话怎样理解,是不是要重新声明个附加属性兵丁到IsLeaf?
just59277 2011-07-12
  • 打赏
  • 举报
回复
在这个双击事件里面
label lab= sender as label;
treeviewitem item=lab.parent.parent as treeviewitem;
if(item.items.count==0)
{
//执行你末节点需要执行的事件
}
沝林 2011-07-12
  • 打赏
  • 举报
回复
你可以为节点绑定的实体创建一个是否叶子节点的标识字段IsLeaf,在创建树时标识这些些节点是否叶子节点,修改附加属性的定义,让它可以接受参数,你可以将IsLeaf传给MouseDoubleClickCommand,在MouseDoubleClickCommand的CanExecute里返回IsLeaf,也就是只有IsLeaf为true时,MouseDoubleClickCommand才有效

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧