Programmatically TreeView selected item set problem
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.
-
sfernandez
Site Admin
- Posts: 3152
- Joined:
Re: Programmatically TreeView selected item set problem
I tried the following sample in Unity:
Then, added a script to programmatically set the selected item:
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?
Code: Select all
<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>
Code: Select all
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);
}
}
Then, if I click another TreeView item, it gets selected correctly.
Is that scenario failing for you?
Re: Programmatically TreeView selected item set problem
Your code is work well. But this modification have a problem (we don't use Buttons and use other EventHandler for Click):
XAML:
Code:
XAML:
Code: Select all
<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: Select all
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);
}
}
-
sfernandez
Site Admin
- Posts: 3152
- Joined:
Re: Programmatically TreeView selected item set problem
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:
Meanwhile you can fix it by focusing the selected item yourself:
Code: Select all
void textBtn_MouseLeftButtonDown(BaseComponent sender, MouseButtonEventArgs e)
{
_item.SetIsSelected(true);
_item.Focus();
}
Re: Programmatically TreeView selected item set problem
The same bug in version 1.2.2.
Re: Programmatically TreeView selected item set problem
Working on it. Please, file a report.
Thanks!
Thanks!
-
sfernandez
Site Admin
- Posts: 3152
- Joined:
Re: Programmatically TreeView selected item set problem
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?
What problem are you experiencing here?
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 1 guest