WPF TreeView在ControlTemplate中 - 处理项目扩展
我正在创建一个自定义控件,在控制模板中,我中有一个TreeView类。
<Style TargetType="{x:Type local:MyControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyControl}">
<Border>
<TreeView ItemsSource="{Binding TreeDataItems, RelativeSource={RelativeSource TemplatedParent}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="TreeViewItem.Expanded">
<i:InvokeCommandAction Command="{Binding TreeItemExpandedCommand, RelativeSource={RelativeSource TemplatedParent}}"
CommandParameter="{Binding}">
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</TreeView>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
在自定义控件的代码文件中,我有此命令:
private ICommand _TreeItemExpandedCommand;
public icommand treeTempandedCommand { 得到 { if(_treeItemExpandedCommand == null) _treeItemExpandEdCommand = new RelayCommand(p =&gt; treeItemExpandExeceCected(p)); 返回_treeItemExpandedCommand; } }
私有void treeTemExpandEdexecected(对象args) { }
我还尝试了
<TreeView TreeViewItem.Expanded="TreeViewItem_Expanded">
...
&lt;/treeview/&gt;
但是都没有被解雇。
如何在自定义控件的代码文件中处理TreeView的扩展事件?
谢谢
I am creating a Custom Control and in the control template I have a TreeView class in it.
<Style TargetType="{x:Type local:MyControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyControl}">
<Border>
<TreeView ItemsSource="{Binding TreeDataItems, RelativeSource={RelativeSource TemplatedParent}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="TreeViewItem.Expanded">
<i:InvokeCommandAction Command="{Binding TreeItemExpandedCommand, RelativeSource={RelativeSource TemplatedParent}}"
CommandParameter="{Binding}">
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</TreeView>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
In the code file for the custom control I have this command:
private ICommand _TreeItemExpandedCommand;
public ICommand TreeItemExpandedCommand
{
get
{
if (_TreeItemExpandedCommand == null)
_TreeItemExpandedCommand = new RelayCommand(p => TreeItemExpandedExecuted(p));
return _TreeItemExpandedCommand;
}
}
private void TreeItemExpandedExecuted(object args)
{
}
I have also tried
<TreeView TreeViewItem.Expanded="TreeViewItem_Expanded">
...
</TreeView/>
but neither gets fired.
How can I handle the TreeView's Expanded event INSIDE my custom control's code file?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不是Pro WPF程序员,但我设法找出解决方案的问题。代码的问题在于,该事件并不完全属于TreeView本身,而是其中的元素(项目属性)。
因此,这种工作无法做到:
另一方面,这将有效:
因为此事件基本上属于Treeview
,所以您需要做的是订阅TreeView中所有TreeViewItems的扩展事件。您可以从后面的代码中执行此操作。
这是我的做法:
首先,我们需要获取树景的控制并在事件中订阅项目:
然后,如果要发射此事,我们知道我们的TreeView中有项目,因此我们可以循环浏览其元素并订阅为了扩展活动:
之后,我们很高兴!我还添加了代码,以便能够从自定义控件外部订阅此事件:
因此,这样,您可以从外部订阅它:
最后,这是.xaml
Update
如果您想要在自定义控件中设置树视图项目,您应该
订阅MyTreeView.loaded事件,以捕获其获得值时的第一次。
我希望它有帮助!
如果有什么不清楚的话,请随时提出,我准备帮助!
I am not pro Wpf programmer, but i manage to figure out the solution for your problem. The problem with your code, is that the event is not exactly belongs to TreeView itself, but rather the elements inside it (Items property).
So this wont work:
On the other hand, this would work:
because this event belongs to TreeView
Basicly, what you need to do is subscribe for the Expanded event of all the TreeViewItems in the TreeView. You can do this from code behind.
Here is how i did it:
First we need to acquire our TreeView Control and subscribe the event if it gets items:
Then, if this is fired, we know that our TreeView has Items in it, so we can cycle through its elements and subscribe for the event Expanded:
After that, we are good to go! I also added code for to be able to subscribe for this event from outside the custom control:
So this way, you can subscribe it from outside like that:
Finally, here it is the .xaml
UPDATE
if you want to set TreeView items inside the custom Control, you should
subscribe the MyTreeView.Loaded event as well to catch the first time when it gets values.
i hope it helps!
If anything is not clear, feel free to ask, i am ready to help!