I currently have an usercontrol with a TreeView and some other things. The TreeView's DataContext is set to the ViewModel and the binding works:
<TreeView Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2"
ItemsSource="{Binding LoadedItem.Batches}"
Background="{Binding UiTools.ContainersBackground}">
<Interactivity:Interaction.Behaviors>
<Behaviros:BindableSelectedItemBehavior SelectedItem="{Binding SelectedSessionItem, Mode=TwoWay}"/>
</Interactivity:Interaction.Behaviors>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Groups}">
<TextBlock Text="{Binding Name, UpdateSourceTrigger=LostFocus}"/>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Samples}">
<TextBox Text="{Binding Name}"/>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate>
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
But now I'm trying to make the nodes editable using this tutorial.
There, for the UserControl some code behind is used, and it now implements INotifyPropertyChanged. The event handler simply is defined:
public event PropertyChangedEventHandler PropertyChanged;
But when it is used, it is always null:
public bool IsInEditMode
{
get { return isInEditMode; }
set
{
isInEditMode = value;
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(nameof(IsInEditMode)));
}
}
}
Here, handler is never called, 'cause it is always null.
I suppose it is because the DataContext of my UserControl is set to the ViewModel and not to the UserControl itself.
When I'd set the DataContext to the UserControl I would not be able to data bind to the ViewModels items.
How could this be solved?
IsInEditModea dependency property so it can be bound by the end user of the control. So the control becomes VM agnostic, which most controls are, and use Gaz's answer as a way to get toIsInEditModeinside the control. - ΩmegaMan