I have the following xaml in my View.
<
tel:RadTreeView
x:Name
=
"UnitTree"
ItemsSource
=
"{Binding Units, Converter={StaticResource UnitHierarchy}}"
SelectedItem
=
"{Binding SelectedUnit, Mode=TwoWay}"
ScrollViewer.VerticalScrollBarVisibility
=
"Auto"
IsDragDropEnabled
=
"True"
BorderBrush
=
"Black"
BorderThickness
=
"1"
Grid.Row
=
"1"
Grid.ColumnSpan
=
"2"
IsExpandOnSingleClickEnabled
=
"True"
>
<
tel:RadTreeView.ItemTemplate
>
<
tel:HierarchicalDataTemplate
ItemsSource
=
"{Binding Converter={StaticResource UnitHierarchy}}"
>
<
StackPanel
Orientation
=
"Vertical"
Width
=
"200"
>
<
StackPanel
Orientation
=
"Horizontal"
>
<
TextBlock
x:Name
=
"name"
TextWrapping
=
"Wrap"
Text
=
"{Binding Name}"
FontWeight
=
"Bold"
/>
<
TextBlock
Text
=
"*"
Margin
=
"10,0,0,0"
/>
</
StackPanel
>
<
TextBlock
x:Name
=
"type"
Text
=
"{Binding UnitType}"
FontStyle
=
"Italic"
FontSize
=
"10"
Foreground
=
"Gray"
FontWeight
=
"Bold"
/>
</
StackPanel
>
</
tel:HierarchicalDataTemplate
>
</
tel:RadTreeView.ItemTemplate
>
</
tel:RadTreeView
>
<
StackPanel
Orientation
=
"Horizontal"
Margin
=
"0,10,0,5"
Grid.Row
=
"2"
Grid.ColumnSpan
=
"2"
HorizontalAlignment
=
"Right"
>
<
Button
Content
=
"Add Child"
Command
=
"{Binding AddChildCommand}"
Style
=
"{StaticResource AppButton}"
/>
<
Button
Content
=
"Remove"
Command
=
"{Binding RemoveCommand}"
Style
=
"{StaticResource AppButton}"
/>
<
Button
Content
=
"Save"
Command
=
"{Binding SaveCommand}"
Style
=
"{StaticResource AppButton}"
/>
</
StackPanel
>
I'm binding to self-referencing data following the technique described here: Binding a treeview to self-referencing data.
In my view model, I have the following:
public
class
UnitNavigatorViewModel : ViewModelBase
{
private
int
newUnitId = -1;
public
RelayCommand AddChildCommand {
get
;
private
set
; }
public
UnitCollection Units {
get
;
set
; }
private
Unit _selectedUnit;
public
Unit SelectedUnit
{
get
{
return
_selectedUnit; }
set
{
if
(_selectedUnit != value)
{
if
(_selectedUnit !=
null
&& (_selectedUnit.HasChanges || _selectedUnit.EntityState == System.ServiceModel.DomainServices.Client.EntityState.New))
{
MessageBox.Show(
"Need to save first."
);
SelectedUnit = _selectedUnit;
}
else
{
var oldValue = _selectedUnit;
_selectedUnit = value;
RaisePropertyChanged(
"SelectedUnit"
, oldValue, value,
true
);
AddChildCommand.RaiseCanExecuteChanged();
RemoveCommand.RaiseCanExecuteChanged();
}
}
}
}
private
void
AddChild()
{
var child =
new
Unit() { Id = newUnitId--, Name =
"New Child Unit"
, ParentId = SelectedUnit.Id };
Units.Add(child);
var unit = SelectedUnit;
Messenger.Default.Send<
string
>(
"ExpandSelectedItem"
);
RaisePropertyChanged(
"Units"
);
SelectedUnit = unit;
SelectedUnit = child;
}
//AddChildCommand is defined as
AddChildCommand =
new
RelayCommand(AddChild, () => SelectedUnit !=
null
);
Lastly, my View code behind looks like this:
public
partial
class
EntityNavigator : UserControl
{
public
EntityNavigator()
{
InitializeComponent();
Messenger.Default.Register<
string
>(
this
, HandleMessage);
}
private
void
HandleMessage(
string
message)
{
if
(message ==
"ExpandSelectedItem"
)
{
this
.UnitTree.SelectedContainer.IsExpanded =
true
;
}
}
}
What I'm trying to accomplish is this. I want to select a node and click add child. I then want the node to expand, display the child and show the child as selected.
The problem I'm having is that the code in the view code behind is throwing an error because the minute I RaisePropertyChanged("Units") to cause the list to be refreshed, SelectedContainer gets set to null. So, trying to set IsExpanded fails.
Thanks for your help!