在WPF中插入后选择一个新的treeViewItem

时间:2013-06-11 13:34:14

标签: c# wpf silverlight

在WPF中,我有一个绑定到分层列表的树视图。当我想将新节点插入树并编辑该新节点的名称(如Windows中的文件夹树视图)时,我无法以编程方式选择新节点。我可以选择父节点,甚至可以在插入后选择新节点。但我想在插入新节点时选择新节点。  

<HierarchicalDataTemplate x:Key="hierarchi" ItemsSource="{Binding Path=Items}" >
        <StackPanel Orientation="Horizontal" Margin="2" MouseLeftButtonDown="Item_MouseLeftButtonDown" Tag="{Binding}" >
            <Image  Name="itemImage" Source="{Binding Path=Image}"  Height="16" Width="16"/>
            <TextBlock Name="textBlockName" VerticalAlignment="Center" Text="{Binding Path=Name}" FontFamily="Tahoma" FontSize="12" />
            <TextBox Name="textBoxName" VerticalAlignment="Center" Text="{Binding Path=Name,Mode=TwoWay}" Visibility="Hidden"  FontFamily="Tahoma" FontSize="12" KeyDown="textBoxName_KeyDown" LostFocus="textBoxName_LostFocus"/>
        </StackPanel>          
</HierarchicalDataTemplate>

我的数据模型是这样的:

public class Model:INotifyPropertyChanged
    {
        //other methods and properties are removed.


        private string name;
        public string Name
        {
            get { return name; }
            set { name = value;
            OnPropertyChange("Name");
            }
        }

        private ObservableCollection items = null;
        public ObservableCollection Items
        {
            get { return items; }
            set { items = value;
            OnPropertyChange("Items");
            }
        }
}

这是添加节点的代码:

private void buttonNew_Click(object sender, RoutedEventArgs e)
    {
        if (treeView.SelectedItem is Model)
        {
            Model parent= (Model)treeView.SelectedItem;

            Model newItem = new Model();
            newItem.Parent = post;
            newItem.Name = "New";
            newItem.ParentID = parent.ID;
            parent.Items.Add(newItem);


            TreeViewItem item = GetTreeViewItem(newItem);

            //problem is here, item is null.
            if(item!=null)
                item.IsSelected = true;
                        }
    }
private TreeViewItem GetTreeViewItem(Model node)
    {
// a recursive method for going to root of the tree and coming back

        if (node.Parent == null)
        {
            DependencyObject dObject = treeView.ItemContainerGenerator.ContainerFromItem(node);
            return (TreeViewItem)dObject;
        }
        TreeViewItem parentItem = GetTreeViewItem(node.Parent);
        if (parentItem == null)
            return null;
        DependencyObject Dependency = parentItem.ItemContainerGenerator.ContainerFromItem(node);
        return (TreeViewItem)Dependency;

    }

我确信我必须更新或渲染treeview数据源,但我不知道如何。

1 个答案:

答案 0 :(得分:1)

您需要在viewmodel中定义IsSelected属性,并在设置属性时调用OnPropertyChanged。 要将IsSelected链接到TreeViewItem,请定义ItemContainerStyle,例如在您的参考资料中或直接在TreeView中。

  <TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
      <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
      <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
          <Setter Property="FontWeight" Value="Bold" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </TreeView.ItemContainerStyle>

完成后,只需设置_model.IsSelected = true

即可