Theres已经发布了关于获取treeview项目的帖子而且他们不适用于我,我的XAML构造方式不同,我的树视图包含在按钮中,所以当我点击任何树视图项目时,我想要' messagebox.show(选择的treeview项目路径)好吗?这很容易理解吗?我搜索的其他方法对我没有用,因为我已经尝试过了我必须重新编写代码才能让它工作但是我不想要它,这里是XAML :
<TreeView x:Name="FolderView" ItemsSource="{Binding Items}" Background="#FF3D3C3C" Margin="0,0,285.667,-0.333">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}">
</Setter>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:item}" ItemsSource="{Binding Children}">
<Button Command="{Binding Source={x:Static local:MainWindow.ClickCommand}}"
CommandParameter="{Binding}">
<StackPanel Orientation="Horizontal" Visibility="{Binding}">
<Image Source="{Binding Type,
Converter={x:Static local:HeaderToImageConverter.Instance}}" Width="20" Margin="3"></Image>
<TextBlock VerticalAlignment="Center" Text="{Binding Name}"></TextBlock>
</StackPanel>
</Button>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
点击此按钮:
public static RoutedUICommand ClickCommand = new RoutedUICommand("ClickCommand", "ClickCommend", typeof(MainWindow));
public MainWindow()
{
this.InitializeComponent();
this.DataContext = new DirectoryStructureViewModel();
CommandBindings.Add(new CommandBinding(ClickCommand, OnClickCommand, OnCanClickCommand));
var i11 = new item() { Name = "aaaa aaaa" };
var i12 = new item() { Name = "bbbb bbbb" };
var i21 = new item() { Name = "cccc cccc" };
var i22 = new item() { Name = "dddd dddd" };
var i0 = new item() { Name = "I1", Children = new List<item>() { i11, i12 } };
var i1 = new item() { Name = "I1", Children = new List<item>() { i21, i22 } };
this.FolderView.ItemsSource = new List<item>() { i0, i1 };
}
private void OnCanClickCommand(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void OnClickCommand(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show(((item)e.Parameter).Name);
}
错误:&#39;无法解析符号&#39;项目&#39;
答案 0 :(得分:0)
SelectedItem
属性应返回绑定项,因此您应该能够:
var object = this.FolderView.SelectedItem as YourType;
if(object != null)
{
MessageBox.Show(object.Name);
}
只需将YourType
替换为您在TreeView中绑定的类型即可
作为旁注,您可以在模板的XAML中使用DataType="YourType"
属性,如果您尝试绑定不在YourType
中的属性,则会抛出编译器错误。
答案 1 :(得分:0)
您应该使用命令。这个例子必须帮助你:
c#code:
public MainWindow()
{
InitializeComponent();
CommandBindings.Add(new CommandBinding(ClickCommand, OnClickCommand, OnCanClickCommand));
var i11 = new Item() { Name = "aaaa aaaa" };
var i12 = new Item() { Name = "bbbb bbbb" };
var i21 = new Item() { Name = "cccc cccc" };
var i22 = new Item() { Name = "dddd dddd" };
var i0 = new Item() { Name = "I1", Children = new List<Item>() { i11, i12 } };
var i1 = new Item() { Name = "I1", Children = new List<Item>() { i21, i22 } };
tv.ItemsSource = new List<Item>() { i0, i1 };
}
private void OnCanClickCommand(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void OnClickCommand(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show(((Item)e.Parameter).Name);
}
public static RoutedUICommand ClickCommand = new RoutedUICommand("ClickCommand", "ClickCommend", typeof(MainWindow));
public class Item
{
public string Name { get; set; }
public List<Item> Children { get; set; }
}
<强>的Xaml:强>
<TreeView Name="tv" >
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:Item}" ItemsSource="{Binding Children}">
<Button Command="{Binding Source={x:Static local:MainWindow.ClickCommand}}"
CommandParameter="{Binding}">
<StackPanel>
<TextBlock VerticalAlignment="Center" Text="{Binding Name}"></TextBlock>
</StackPanel>
</Button>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>