【发布时间】:2014-09-18 13:16:11
【问题描述】:
我的Node 班级:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
namespace FrontEnd
{
public enum NodeType
{
SQLite,
Database,
TableCollection,
ViewCollection,
IndexCollection,
TriggerCollection,
ColumnCollection,
Table,
View,
Column,
Index,
Trigger
}
public class Node
{
public string Title { get; protected set; }
public NodeType Type { get; protected set; }
public ObservableCollection<Node> Nodes { get; set; }
public Node(string title, NodeType type)
{
this.Title = title;
this.Type = type;
this.Nodes = new ObservableCollection<Node>();
}
}
}
我的 XAML:
<TreeView Name="dbTree" Padding="0,5,0,0">
<TreeView.Resources>
<ContextMenu x:Key="ScaleCollectionPopup">
<MenuItem Header="New Scale..."/>
</ContextMenu>
<ContextMenu x:Key="ScaleItemPopup">
<MenuItem Header="Remove Scale"/>
</ContextMenu>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Style.Triggers>
<DataTrigger Binding="{Binding Type, RelativeSource={RelativeSource Self}}" Value="NodeType.Column">
<Setter Property="ContextMenu" Value="{StaticResource ScaleItemPopup}" />
</DataTrigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Nodes}">
<StackPanel Orientation="Horizontal" Margin="0,0,0,4">
<Image Source="{Binding Converter={StaticResource StringToImageConverter}}" />
<TextBlock Text="{Binding Title}" Padding="5,0,0,0" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
我想要实现和失败的是根据绑定的Node 类的Type 属性来决定要使用的ContextMenu。
如果是表或视图,我想显示“SELECT 1000 ROWS”和“SHOW CREATE SQL”,对于其他类型,我想定义其他选项。
达到预期效果的正确方法是什么?
【问题讨论】:
-
数据模板和数据触发器可以帮助您根据需要切换菜单
-
@pushpraj 如您所见,这是我上面的方法,但它不起作用,我希望有人能解释我的错误。
-
Treeview的ItemsSource是什么?也试试
<DataTrigger Binding="{Binding Type}" Value="Column">。也许检查您在输出窗口中看到的任何数据绑定错误。如果需要,您也可以尝试将资源作为样式资源移动。
标签: c# wpf data-binding enums triggers