pmike
Topic Author
Posts: 28
Joined: 31 Oct 2012, 09:29

Programmatically TreeView selected item set problem

27 Jun 2014, 08:52

When you call a method TreeViewItem SetIsSelected(true) occurs visual update the selected tree item, but then when you click the mouse on another element of the tree does not change the selected item. This is fixed only if again click on selected items and then all is well.
 
User avatar
sfernandez
Site Admin
Posts: 1918
Joined: 22 Dec 2011, 19:20

Re: Programmatically TreeView selected item set problem

01 Jul 2014, 14:59

I tried the following sample in Unity:
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <TreeView>
            <TreeViewItem Header="A">
                <TreeViewItem Header="A.1"/>
                <TreeViewItem Header="A.2"/>
            </TreeViewItem>
            <TreeViewItem Header="B">
                <TreeViewItem Header="B.1"/>
                <TreeViewItem Header="B.2">
                    <TreeViewItem Header="i"/>
                    <TreeViewItem Header="ii"/>
                    <TreeViewItem Header="iii"/>
                </TreeViewItem>
                <TreeViewItem Header="B.3"/>
            </TreeViewItem>
            <TreeViewItem x:Name="item" Header="C"/>
            <TreeViewItem Header="D">
                <TreeViewItem Header="D.1"/>
            </TreeViewItem>
        </TreeView>
        <Button x:Name="btn" Content="Select TreeView Item"/>
    </StackPanel>
    
</Grid>
Then, added a script to programmatically set the selected item:
using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour
{
    Noesis.TreeViewItem _item;
    
    void Start()
    {
        var gui = GetComponent<NoesisGUIPanel>();
        var root = gui.GetRoot<Noesis.Grid>();
        var btn = root.FindName<Noesis.Button>("btn");
        btn.Click += this.OnBtnClick;
        _item = root.FindName<Noesis.TreeViewItem>("item");
    }

    void OnBtnClick(Noesis.BaseComponent sender, Noesis.RoutedEventArgs e)
    {
        _item.SetIsSelected(true);
    }
}
When I click the button, "C" item gets selected.
Then, if I click another TreeView item, it gets selected correctly.

Is that scenario failing for you?
 
pmike
Topic Author
Posts: 28
Joined: 31 Oct 2012, 09:29

Re: Programmatically TreeView selected item set problem

01 Jul 2014, 16:20

Your code is work well. But this modification have a problem (we don't use Buttons and use other EventHandler for Click):

XAML:
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
 
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <TreeView>
            <TreeViewItem Header="A">
                <TreeViewItem Header="A.1"/>
                <TreeViewItem Header="A.2"/>
            </TreeViewItem>
            <TreeViewItem Header="B">
                <TreeViewItem Header="B.1"/>
                <TreeViewItem Header="B.2">
                    <TreeViewItem Header="i"/>
                    <TreeViewItem Header="ii"/>
                    <TreeViewItem Header="iii"/>
                </TreeViewItem>
                <TreeViewItem Header="B.3"/>
            </TreeViewItem>
            <TreeViewItem x:Name="item" Header="C"/>
            <TreeViewItem Header="D">
                <TreeViewItem Header="D.1"/>
            </TreeViewItem>
        </TreeView>
        <TextBlock x:Name="btn" Text="Select TreeView Item"/>
    </StackPanel>
</Grid>
Code:
using UnityEngine;
using System.Collections;
using Noesis;
using System;

public class Test : MonoBehaviour
{
    Noesis.TreeViewItem _item;
   
    void Start()
    {
        var gui = GetComponent<NoesisGUIPanel>();
        var root = gui.GetRoot<Noesis.Grid>();
        var textBtn = root.FindName<Noesis.TextBlock>("btn");
        _item = root.FindName<Noesis.TreeViewItem>("item");

        textBtn.MouseLeftButtonDown += textBtn_MouseLeftButtonDown;               
    }

    void textBtn_MouseLeftButtonDown(BaseComponent sender, MouseButtonEventArgs e)
    {
        _item.SetIsSelected(true);
    }
}
 
User avatar
sfernandez
Site Admin
Posts: 1918
Joined: 22 Dec 2011, 19:20

Re: Programmatically TreeView selected item set problem

06 Jul 2014, 12:19

It is a problem of the focus and how we implemented the selection of the TreeViewItem. We will fix it in the next release.

Meanwhile you can fix it by focusing the selected item yourself:
void textBtn_MouseLeftButtonDown(BaseComponent sender, MouseButtonEventArgs e)
{
    _item.SetIsSelected(true);
    _item.Focus();
}
 
pmike
Topic Author
Posts: 28
Joined: 31 Oct 2012, 09:29

Re: Programmatically TreeView selected item set problem

07 Jul 2014, 08:29

ОК! Thanks! ;)
 
pmike
Topic Author
Posts: 28
Joined: 31 Oct 2012, 09:29

Re: Programmatically TreeView selected item set problem

07 Apr 2015, 11:29

The same bug in version 1.2.2.
 
User avatar
jsantos
Site Admin
Posts: 2906
Joined: 20 Jan 2012, 17:18
Contact:

Re: Programmatically TreeView selected item set problem

08 Apr 2015, 20:44

Working on it. Please, file a report.

Thanks!
 
User avatar
sfernandez
Site Admin
Posts: 1918
Joined: 22 Dec 2011, 19:20

Re: Programmatically TreeView selected item set problem

21 Apr 2015, 12:06

I've been trying (with 1.2.2 version) the sample code you posted (the one using the MouseLeftButtonDown over a TextBlock) and I cannot reproduce the error you describe in the first post. Everytime I click in the TextBlock, the item named "C" is selected, and then I can click any other item of the TreeView and it gets correctly selected. I don't need to click two times on the item to make it selected.

What problem are you experiencing here?

Who is online

Users browsing this forum: Google [Bot] and 1 guest