WPF Treeview 样式 EventSetter 不起作用

编程入门 行业动态 更新时间:2024-10-25 18:34:24
本文介绍了WPF Treeview 样式 EventSetter 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好,我的事件设置器有问题.我的窗口:

Hallo i have a problem with an eventsetter. My Window:

<TreeView.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="CrefoChartTreeViewItemStyle.xaml" /> </ResourceDictionary.MergedDictionaries> <HierarchicalDataTemplate DataType="{x:Type local:Node}" ItemsSource="{Binding ChildNodes}"> </HierarchicalDataTemplate> </ResourceDictionary> </TreeView.Resources>

我的 CrefoChartTreeViewItemStyle.xaml

My CrefoChartTreeViewItemStyle.xaml

<ResourceDictionary xmlns="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/winfx/2006/xaml" xmlns:d="schemas.microsoft/expression/blend/2008" xmlns:mc="schemas.openxmlformats/markup-compatibility/2006" xmlns:ed="schemas.microsoft/expression/2010/drawing" mc:Ignorable="d" > <Style TargetType="TreeViewItem"> <Style.Resources> <LinearGradientBrush x:Key="ButtonBrush" EndPoint="0,1" StartPoint="0,0"> <GradientStop Color="White" Offset="0.25"/> <GradientStop Color="#FFA5DBE9" Offset="1"/> </LinearGradientBrush> <Style x:Key="ButtonStyle" TargetType="{x:Type Button}"> <Setter Property="Background" Value="{DynamicResource ButtonBrush}" /> <EventSetter Event="Click" Handler="ButtonOnClick" /> </Style> </Style.Resources>

我在编译时收到错误消息:

I get the Error Message when i Compile:

The event 'click' can not be specified on a Target tag in a Style. Instead, use "EventSetter".

我做错了什么?

有没有其他方法可以触发树视图中的这个按钮?所以我可以把代码放在后面?

Is there any other way to get this button in the treeview triggerd? so i can put Code behind?

推荐答案

这行不通,因为资源 xaml 不能有代码隐藏文件,它们通常被称为loose xaml"".您可以在有关 EventSetter 的 msdn 中阅读相关内容.您可以并且应该做的是使用将您的事件转换为命令的东西,例如 AttachedCommandBehavior 这与 MVVM 一起工作得非常好.如果你想使用你所要求的事件,你可以将 TreeView 放在一个 UserControl 中,然后你就可以使用事件了.

This won't work, Because a resource xaml can't have a code behind file, they are usually called "loose xaml". You can read about that in the msdn about EventSetter. What you can and should do is to use something that converts your events to commands, like the AttachedCommandBehavior this works very nicely together with MVVM. If you want to use events like you asked for, you can place the TreeView in a UserControl, and then you can use events.

更多推荐

WPF Treeview 样式 EventSetter 不起作用

本文发布于:2023-11-11 18:41:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579260.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:样式   不起作用   WPF   Treeview   EventSetter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!