none
TreeViewItem Binding RRS feed

  • Question

  • I am using a datatemplate to bind data to treeviewitem.  When I do this, the ability to select and item does not work; there is no background color change when clicking on a node (but the mouse over does).  Here is sample code which builds a treeview in XAML that works fine and one the is bound and does not work.

    Here is the xaml

    <Window.Resources>
        <DataTemplate x:Key="LastNodeTemplate">
          <TreeViewItem Header="{Binding Child}">
          </TreeViewItem>
        </DataTemplate>

        <HierarchicalDataTemplate
          x:Key="2TierTreeTemplate"
          ItemsSource="{Binding Parent2Child}"
          ItemTemplate="{StaticResource LastNodeTemplate}">
          <TreeViewItem Header="{Binding Parent}"/>
        </HierarchicalDataTemplate>
       
        <Style TargetType="TreeViewItem">
          <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
              <Setter Property="Background" Value="Yellow" />
            </Trigger>
          </Style.Triggers>
        </Style>
       
      </Window.Resources>
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition/>
          <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TreeView Grid.Column="0">
          <TreeViewItem Header="One">
            <TreeViewItem Header="One"/>
            <TreeViewItem Header="Two"/>
            <TreeViewItem Header="Three"/>
          </TreeViewItem>
          <TreeViewItem Header="Two">
            <TreeViewItem Header="One"/>
            <TreeViewItem Header="Two"/>
            <TreeViewItem Header="Three"/>
          </TreeViewItem>
          <TreeViewItem Header="Three">
            <TreeViewItem Header="One"/>
            <TreeViewItem Header="Two"/>
            <TreeViewItem Header="Three"/>
          </TreeViewItem>
        </TreeView>

        <TreeView Name="treeBind"
                  Grid.Column="1"
                  ItemsSource="{Binding Parent}"
                  ItemTemplate="{StaticResource 2TierTreeTemplate}">
        </TreeView>
      </Grid>

    and here is the code behind

    public partial class Window1 : Window
      {
        public Window1()
        {
          InitializeComponent();
          treeBind.DataContext = GetSet();
        }

        private DataSet GetSet()
        {
          DataSet dsTemp = new DataSet();
          DataTable dtParent = new DataTable("Parent");
          DataTable dtChild = new DataTable("Child");
          string[] sInfo = new string[2];

          dtParent.Columns.Add("Parent");
          dtParent.Columns.Add("ID", typeof(decimal));
          sInfo[0] = "One";
          sInfo[1] = "1";
          dtParent.Rows.Add(sInfo);
          sInfo[0] = "Two";
          sInfo[1] = "2";
          dtParent.Rows.Add(sInfo);
          sInfo[0] = "Three";
          sInfo[1] = "3";
          dtParent.Rows.Add(sInfo);

          dtChild.Columns.Add("Child");
          dtChild.Columns.Add("ID", typeof(decimal));
          sInfo[0] = "One";
          sInfo[1] = "1";
          dtChild.Rows.Add(sInfo);
          sInfo[0] = "Two";
          sInfo[1] = "1";
          dtChild.Rows.Add(sInfo);
          sInfo[0] = "Three";
          sInfo[1] = "1";
          dtChild.Rows.Add(sInfo);
          sInfo[0] = "One";
          sInfo[1] = "2";
          dtChild.Rows.Add(sInfo);
          sInfo[0] = "Two";
          sInfo[1] = "2";
          dtChild.Rows.Add(sInfo);
          sInfo[0] = "Three";
          sInfo[1] = "2";
          dtChild.Rows.Add(sInfo);
          sInfo[0] = "One";
          sInfo[1] = "3";
          dtChild.Rows.Add(sInfo);
          sInfo[0] = "Two";
          sInfo[1] = "3";
          dtChild.Rows.Add(sInfo);
          sInfo[0] = "Three";
          sInfo[1] = "3";
          dtChild.Rows.Add(sInfo);

          dsTemp.Tables.Add(dtParent);
          dsTemp.Tables.Add(dtChild);
          dsTemp.Relations.Add("Parent2Child", dsTemp.Tables["Parent"].Columns["ID"],
            dsTemp.Tables["Child"].Columns["ID"], false);
          return dsTemp;
        }
      }

    Any help would be appreciated,

    Stephen
    Wednesday, November 4, 2009 1:49 PM

Answers

  • Part of the problem is that you are defining your data templates with TreeViewItems:

    <DataTemplate x:Key="LastNodeTemplate">
          <TreeViewItem Header="{Binding Child}">
          </TreeViewItem>

    </DataTemplate>

    <HierarchicalDataTemplate
          x:Key="2TierTreeTemplate"
          ItemsSource="{Binding Parent2Child}"
          ItemTemplate="{StaticResource LastNodeTemplate}">
          <TreeViewItem Header="{Binding Parent}"/>
    </HierarchicalDataTemplate>

    The data templates define how the data is bound, and how it is presented. The TreeView will automatically generate TreeViewItems using your data templates. Remove the TreeViewItems from the templates as follows:

    <DataTemplate x:Key="LastNodeTemplate">
          <TextBlock Text="{Binding Child}" />
    </DataTemplate>

    <HierarchicalDataTemplate
          x:Key="2TierTreeTemplate"
          ItemsSource="{Binding Parent2Child}"
          ItemTemplate="{StaticResource LastNodeTemplate}">
          <TextBlock Text="{Binding Parent}"/>
    </HierarchicalDataTemplate>

    • Marked as answer by skwishst Wednesday, November 4, 2009 5:19 PM
    Wednesday, November 4, 2009 3:32 PM