I currently have a WPF Treeview with a structure of:
--> Group 1 (Hierarchical Group)
---> Group 2 (Hierarchical Group)
---> Item 1
---> Item 2
---> Item 3
I currently have a SelectedItemChanged event handler hooked up to this treeview like so
private void TreeViewControl_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
if (e.NewValue is Item)
{
Item item = e.NewValue as Item;
if (Item != SelectedItem)
{
//keep SelectedItem in sync with Treeview.SelectedItem
SelectedItem = e.NewValue as Item;
}
}
else
{
//if the user tries to select an object that isn't an Item (i.e. a group) reselect the first Item in that group
//This will then cause stack overflow in methods I've tried so far
}
}
So my question is, how can I keep the SelectedItem of the treeview in sync with my SelectedItem property in my code behind and also how can I reselect an item if the user selects a group?
EDIT:
<TreeView Grid.Row="1" Grid.RowSpan="2"
ItemContainerStyle="{StaticResource StandardListStyle}"
ItemTemplate="{StaticResource TreeViewItemTemplate}"
BorderThickness="1,0,1,1"
VerticalContentAlignment="Stretch"
HorizontalAlignment="Stretch"
ItemsSource="{Binding Teams}"
SelectedItemChanged="TreeViewControl_SelectedItemChanged"
Loaded="OnTreeViewLoaded"
x:Name="TreeViewControl">
where:
Teams = List<HierachicalGroup>;
and where:
public class HierachicalGroup
{
public virtual string Name { get; set; }
public virtual HierachicalGroup[] Children { get; set; }
public virtual HierachicalGroup Parent { get; set; }
}
and item is:
public class Item: HierachicalGroup
{
public Domain Domain { get; set; }
public override string Name
{
get
{
return Domain.DomainName;
}
}
}