【问题标题】:WPF Treeview Style EventSetter dont workWPF Treeview Style EventSetter 不起作用
【发布时间】:2012-11-26 10:04:24
【问题描述】:

您好,我的事件设置器有问题。
我的窗口:

<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

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ed="http://schemas.microsoft.com/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>  

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

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

我做错了什么?

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

【问题讨论】:

    标签: wpf treeview styles eventsetter


    【解决方案1】:

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

    【讨论】:

    • 我不同意 - 资源字典 xamls 可以 有一个代码隐藏文件。您可以在this SO questionthis SO question(可能还有更多)中了解它。
    • @O.R.Mapper 好吧,你是对的,如果你走得那么远,这是可能的。但我会说这不是常规情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多