Page 1 of 1

Programmatically TreeView selected item set problem

Posted: 27 Jun 2014, 08:52
by pmike
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.

Re: Programmatically TreeView selected item set problem

Posted: 01 Jul 2014, 14:59
by sfernandez
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?

Re: Programmatically TreeView selected item set problem

Posted: 01 Jul 2014, 16:20
by pmike
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);
    }
}

Re: Programmatically TreeView selected item set problem

Posted: 06 Jul 2014, 12:19
by sfernandez
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();
}

Re: Programmatically TreeView selected item set problem

Posted: 07 Jul 2014, 08:29
by pmike
ОК! Thanks! ;)

Re: Programmatically TreeView selected item set problem

Posted: 07 Apr 2015, 11:29
by pmike
The same bug in version 1.2.2.

Re: Programmatically TreeView selected item set problem

Posted: 08 Apr 2015, 20:44
by jsantos
Working on it. Please, file a report.

Thanks!

Re: Programmatically TreeView selected item set problem

Posted: 21 Apr 2015, 12:06
by sfernandez
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?