我有一个WPF TreeView
,它是在运行时从XML文件填充的。我单击一个按钮,出现一个FileDialog
框,然后选择一个XML文件。然后将XML文件加载到TreeView
中。我使用了一个XmlDataProvider
和一些HierarchicalDataTemplate
来加载XML。
我已经为TreeView添加了一个操作处理程序,但当我选择该TreeView的一个项时,我找不到该项的任何引用。我只找到第一个元素的引用。
我该如何解决这个问题?
XAML是:
<Window x:Class="Demo2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="600" Width="800">
<Window.Resources>
<XmlDataProvider x:Key="MEIInformation" XPath="/MEI" />
<HierarchicalDataTemplate DataType="Case" ItemsSource="{Binding}">
<TextBlock Text="{Binding XPath=@Name}"></TextBlock>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="Phase" ItemsSource="{Binding}">
<TextBlock Text="{Binding XPath=@Name}"></TextBlock>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="Trigger" ItemsSource="{Binding}">
<TextBlock Text="{Binding XPath=@Name}"></TextBlock>
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<ToolBarTray>
<ToolBar>
<Button ToolTip="Open Test Suite" Click="OpenTestSuite">
<Image Source="Icons/open.png"></Image>
</Button>
</ToolBar>
</ToolBarTray>
<TreeView x:Name="trv" FontSize="14" Height="518" HorizontalAlignment="Left" Margin="6,31,0,0" VerticalAlignment="Top" Width="431">
<TreeViewItem ItemsSource="{Binding Source={StaticResource MEIInformation}, XPath=*}" Header="Suites"></TreeViewItem>
</TreeView>
</Grid>
</Window>
而最关键的代码是:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.trv.MouseRightButtonUp+=new MouseButtonEventHandler(DoSomething);
}
private void OpenTestSuite(object sender, RoutedEventArgs e)
{
XmlDocument xmlDocument = new XmlDocument();
OpenFileDialog open = new OpenFileDialog();
open.Filter = "XML Files (*.xml)|*.xml";
if (open.ShowDialog() == true)
{
xmlDocument.Load(open.FileName);
XmlDataProvider dataProvider = this.FindResource("MEIInformation") as XmlDataProvider;
dataProvider.Document = xmlDocument;
}
}
private void DoSomething(object sender, MouseEventArgs e)
{
MessageBox.Show("Do Something in TreeView!");
TreeViewItem childItem = e.Source as TreeViewItem;
if (childItem != null)
{
MessageBox.Show(childItem.Header.ToString()); // or MessageBox.Show(childItem.toString);
childItem.IsSelected = true;
}
else
MessageBox.Show("No Selected Item!");
}
}
XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<MEI>
<Case Name="Jean Price">
<Phase Name="January">
<Trigger Name="Order # JAN001"></Trigger>
<Trigger Name="Order # JAN002"></Trigger>
</Phase>
<Phase Name="February">
<Trigger Name="Order # FEB001"></Trigger>
</Phase>
</Case>
<Case Name="John P Grant">
<Phase Name="April">
<Trigger Name="Order # APR001"></Trigger>
<!-- ... -->
</MEI>
我只想有一个该项目的引用,这样我就可以在该项目之后添加新项目。
@Mart我已经这样改变了xaml上的树视图。。。(已添加SelectedItemChanged="DoSomething")。。。
<TreeView SelectedItemChanged="DoSomething"....../>
c#代码现在是。。。。。。。
private void DoSomething(object sender, EventArgs e)
{
MessageBox.Show("Do Something in TreeView!");
TreeViewItem childItem = sender as TreeViewItem;
if (childItem != null)
{
MessageBox.Show(childItem.Header.ToString());
childItem.IsSelected = true;
MessageBox.Show("It Works!");
}
else
MessageBox.Show("No Selected Item!");
}
但是一点运气都没有。我还没有理解你的UIElement建议…:(
这是我找到的获取selectedTreeViewItem
值的方法,我从xmlElement
中获得了它。
考虑到这个xaml代码:
<i>
<Window.Resources>
<HierarchicalDataTemplate DataType="Filial"
ItemsSource="{Binding XPath=./*}">
<StackPanel Orientation="Horizontal">
<Image Source="/WpfClient;component/Images/image4.png"
Height="20"></Image>
<TextBlock Margin="5,0,0,0"
Text="{Binding XPath=@name}"
FontSize="18"
FontFamily="BankGothic Md BT"
Foreground="#FF355CE5"/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="Setor"
ItemsSource="{Binding XPath=./*}">
<StackPanel Orientation="Horizontal">
<TextBlock Margin="5,0,0,0"
Text="{Binding XPath=@name}"
FontWeight="Bold" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="User">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding XPath=@source}"
Height="15"></Image>
<TextBlock Margin="5,0,0,0"
Text="{Binding XPath=@name}"
FontStyle="Italic" />
</StackPanel>
</HierarchicalDataTemplate>
<XmlDataProvider x:Key="xmlDP"
Source="Contatos.xml"
XPath="/Contatos/Filial"></XmlDataProvider>
</Window.Resources>
<Grid Width="Auto"
Height="Auto">
<TreeView Name="tv"
ItemsSource="{Binding }"
DataContext="{StaticResource xmlDP}"
Margin="0,0,0,0"
TreeViewItem.Selected="tv_Selected">
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="TreeViewItem.IsExpanded"
Value="True" />
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
</Grid>
</i> </pre>
并且这个c#codebehind:
private void tv_Selected(object sender, RoutedEventArgs e)
{
System.Xml.XmlElement xmlElement= (XmlElement)tv.SelectedItem;
string mySelectedValue = xmlElement.Attributes[0].Value.ToString();
MessageBox.Show(mySelectedValue , "SelectedTreeValue", MessageBoxButton.OK, MessageBoxImage.Information);
}
</i>
这是xml文件:
<i>
<?xml version="1.0" encoding="utf-8" ?>
<Contatos>
<Filial name="Firm">
<Setor name="Sector 1">
<User name="user1" source="/WpfClient;component/Images/user.png"></User>
<User name="user2" source="/WpfClient;component/Images/user.png"></User>
<User name="user3" source="/WpfClient;component/Images/admin.png"></User>
</Setor>
<Setor name="Sector40">
<User name="user43" source="/WpfClient;component/Images/admin.png"></User>
<User name="user44" source="/WpfClient;component/Images/user.png"></User>
<User name="user45" source="/WpfClient;component/Images/user.png"></User>
<User name="user46" source="/WpfClient;component/Images/user.png"></User>
</Setor>
</Filial>
</Contatos>
</i>
我看不出是什么触发了DoSomething
处理程序,我想它会响应Click
事件。
如果它是项目模板的单击元素,则可能无法获得正确的发件人参数。
您可以将发送方强制转换为UIElement
,然后访问它的DataContext
,它应该是您的XML元素之一。
另一种方法是对树视图的SelectionChanged
事件作出反应,这将在事件参数中为您提供treview项。