首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

"TreeView"数据绑定解决方法

2012-08-11 
TreeView数据绑定在“Accordion控件”下使用了“TreeView”控件,但在对“TreeView控件”进行绑定时出现了问题。

"TreeView"数据绑定
在“Accordion控件”下使用了“TreeView”控件,但在对“TreeView控件”进行绑定时出现了问题。

一、XAML代码:

  <ComboBox Height="23" HorizontalAlignment="Left" Margin="242,12,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120" >
  <ComboBox.ItemTemplate>
  <DataTemplate>
  <StackPanel>
  <TextBlock Text="请选择" />
  </StackPanel>
  </DataTemplate>
  </ComboBox.ItemTemplate>
  <ComboBoxItem>

  <layoutToolkit:Accordion x:Name="theTreeView" SelectionMode="ZeroOrMore" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" Margin="5" BorderBrush="#FF849AAD" Height="Auto" BorderThickness="1">
  <layoutToolkit:Accordion.ItemTemplate>
  <DataTemplate>
  <StackPanel>
<TextBlock Text="{Binding CatalogName}" />
  </StackPanel>
  </DataTemplate>
  </layoutToolkit:Accordion.ItemTemplate>
  <layoutToolkit:Accordion.ContentTemplate>
  <DataTemplate>
  <sdk:TreeView x:Name="TreeOfLife" Margin="5" Grid.Column="0" Grid.Row="1">
  </sdk:TreeView>

  问题一:“TreeView”位于“Accordion”中,“x:Name”不能这样指定吧? 就算指定了(比如这里),后台代码“TreeOfLife.Items.Add(objTreeNode);”也提示找不到“TreeOfLife”,为什么?

问题二:“x:Name”不能进行绑定是么? x:Name=“{Binding Name}”,这样绑定提示错误。那改怎样将“Name”属性绑定到“TreeView”呢??? 方便在后台代码中获取该“TreeView”元素,进行数据绑定。

问题三:如何定义"Accordion"下每个“TreeView”元素的节点单击事件,方便处理?
  </DataTemplate>
  </layoutToolkit:Accordion.ContentTemplate>
  </layoutToolkit:Accordion>
  </ComboBoxItem>
  </ComboBox>

二、后台代码:


namespace CustomMenu
{
  public partial class ComboList : UserControl
  {
  List<ForumInfo> forumList = new List<ForumInfo>();
  public ComboList()
  {
  InitializeComponent();
  BindDataSource();
  }
  public void BindDataSource()
  {
  ObservableCollection<Catalog> catalogsk = new ObservableCollection<Catalog>();
  string BlackStr = "";
  catalogsk.Add(
  new Catalog()
  {
  CatalogName = "建筑一",
  TestSimples = new ObservableCollection<TestSimple>() 
  { 
  new TestSimple(){Name=BlackStr+"楼层一",TestID="1001"},
  new TestSimple(){Name=BlackStr+"楼层二",TestID="1002"},
  new TestSimple(){Name=BlackStr+"楼层三",TestID="1003"},
  new TestSimple(){Name=BlackStr+"楼层四",TestID="1004"},
  }
  }
  );


  catalogsk.Add(
  new Catalog()
  {
  CatalogName = "建筑二",
  TestSimples = new ObservableCollection<TestSimple>() 
  { 
  new TestSimple(){Name=BlackStr+"楼层一",TestID="2001"},
  new TestSimple(){Name=BlackStr+"楼层二",TestID="2002"},
  new TestSimple(){Name=BlackStr+"楼层三",TestID="2003"},
  new TestSimple(){Name=BlackStr+"楼层四",TestID="2004"},
  new TestSimple(){Name=BlackStr+"楼层五",TestID="2005"},
  new TestSimple(){Name=BlackStr+"楼层六",TestID="2006"},
  }
  }
  );
  catalogsk.Add(
  new Catalog()
  {
  CatalogName = "建筑三",
  TestSimples = new ObservableCollection<TestSimple>() 
  { 
  new TestSimple(){Name=BlackStr+"楼层一",TestID="3001"},
  new TestSimple(){Name=BlackStr+"楼层二",TestID="3002"},
  new TestSimple(){Name=BlackStr+"楼层三",TestID="3003"},
  new TestSimple(){Name=BlackStr+"楼层四",TestID="3004"},
  new TestSimple(){Name=BlackStr+"楼层五",TestID="3005"},
  new TestSimple(){Name=BlackStr+"楼层六",TestID="3006"},
  }
  }
  );
   
  theTreeView.ItemsSource = catalogsk;

  try
  {
  forumList =CustomMenu.DateService.GetForumData();
  AddTreeNode(0, null);
  }
  catch
  {
  throw new NotImplementedException();
  }
  }

  private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  {
  TextBlock tb = sender as TextBlock;
  MessageBox.Show(tb.Tag.ToString());
  }


  private void AddTreeNode(int parentID, TreeViewItem treeViewItem)
  {
  List<ForumInfo> result = (from forumInfo in forumList
  where forumInfo.ParendID == parentID
  select forumInfo).ToList<ForumInfo>();

  if (result.Count > 0)
  {
  foreach (ForumInfo foruminfo in result)
  {
  TreeViewItem objTreeNode = new TreeViewItem();
  objTreeNode.Header = foruminfo.ForumName;
  objTreeNode.DataContext = foruminfo;

  //此样式将会添加的所有叶子结点上
  objTreeNode.ItemContainerStyle = this.Resources["RedItemStyle"] as Style;
  //objTreeNode.IsSelected = true;
  //objTreeNode.IsEnabled = false;
  //objTreeNode.IsExpanded = true;
  //添加根节点


  if (treeViewItem == null)
  {
  //TreeOfLife.Items.Add(objTreeNode);
  }
  else
  {
  treeViewItem.Items.Add(objTreeNode);
  }
  AddTreeNode(foruminfo.ForumID, objTreeNode);
  }
  }
  }

  public class Catalog
  {
  public Catalog()
  {
  TestSimples = new ObservableCollection<TestSimple>();
  }

  public string CatalogName { get; set; }
  public ObservableCollection<TestSimple> TestSimples { get; set; }
  }

  public class TestSimple
  {
  public string Name { get; set; }
  public string TestID { get; set; }
  }
  }


  public class ForumInfo
  {
  public int ForumID { get; set; }
  public int ParendID { get; set; }
  public string ForumName { get; set; }
  }

  public class DateService
  {
  public static List<ForumInfo> GetForumData()
  {
  List<ForumInfo> forumList = new List<ForumInfo>();
  forumList.Add(new ForumInfo() { ForumID = 1, ParendID = 0, ForumName = "笔记本版块" });
  forumList.Add(new ForumInfo() { ForumID = 2, ParendID = 0, ForumName = "台式机版块" });

  forumList.Add(new ForumInfo() { ForumID = 3, ParendID = 1, ForumName = "Dell笔记本" });
  forumList.Add(new ForumInfo() { ForumID = 4, ParendID = 1, ForumName = "IBM笔记本" });
  forumList.Add(new ForumInfo() { ForumID = 5, ParendID = 4, ForumName = "IBM-T系列" });
  forumList.Add(new ForumInfo() { ForumID = 6, ParendID = 4, ForumName = "IBM-R系列" });

  forumList.Add(new ForumInfo() { ForumID = 7, ParendID = 2, ForumName = "联想台式机" });
  forumList.Add(new ForumInfo() { ForumID = 8, ParendID = 2, ForumName = "方正台式机" });
  forumList.Add(new ForumInfo() { ForumID = 9, ParendID = 2, ForumName = "HP台式机" });
  forumList.Add(new ForumInfo() { ForumID = 10, ParendID = 7, ForumName = "联想家悦H系列" });
  forumList.Add(new ForumInfo() { ForumID = 11, ParendID = 7, ForumName = "联想IdeaCentre系列" });

  return forumList;
  }
  }
}
三、绑定之后的图

   


谢谢,各位侠士们了。。。

[解决办法]
问题一:“TreeView”位于“Accordion”中,“x:Name”不能这样指定吧? 就算指定了(比如这里),后台代码“TreeOfLife.Items.Add(objTreeNode);”也提示找不到“TreeOfLife”,为什么?
后台代码不能通过控件name获取到模板中的控件
建议使用绑定实现,把一个list绑定到treeview控件,然后操作list的数据,得到你的效果


 问题二:“x:Name”不能进行绑定是么? x:Name=“{Binding Name}”,这样绑定提示错误。那改怎样将“Name”属性绑定到“TreeView”呢??? 方便在后台代码中获取该“TreeView”元素,进行数据绑定。
你不需要获取view,mvvm模式中,view依赖viewmodel,vm是不考虑view的。你想做什么在viewmodel中做,然后把对应的属性绑定到view的对应控件上


 问题三:如何定义"Accordion"下每个“TreeView”元素的节点单击事件,方便处理?
</DataTemplate>
</layoutToolkit:Accordion.ContentTemplate>
</layoutToolkit:Accordion>
</ComboBoxItem>
</ComboBox>

其实你就是一个问题,使用绑定,一切都可以解决,这个问题相同,使用command即可
[解决办法]


楼上说的对,绑定就可以,如果不喜欢command可以使用treeview的datatemplate里面的控件来写事件,最好是在datatemplate的根元素写事件

热点排行