WPF MVVM TreeView item source losing context after command

Posted by user3955716 on Stack Overflow See other posts from Stack Overflow or by user3955716
Published on 2014-08-19T10:14:22Z Indexed on 2014/08/19 10:20 UTC
Read the original article Hit count: 194

Filed under:
|
|

I have a treeview which contains files, every view model holds an item source which is an ObservableCollection with files items:

public ObservableCollection<CMItemFileNode> SubItemNode

On each item i have context menu options (Delete, Execute..). If i move from one viewModel to another the ObservableCollection of files updated correctly and presented correctly but, when i perform a context menu command like delete file item, the command execute good but when i move to another view model (which holds SubItemNode ObservableCollection of is own) after the command executed the WPF still thinks i'm in the last view model i was in and not the one i'm really on.

Very important to mention is that when i update to .net 4.5 (which unfortunantly i can't do) everything is ok and the ObservableCollection addresses the correct view model.

Here is the treeView:

<TreeView x:Name="Files" Margin="0,5,5,0" Grid.Row="6" Grid.Column="2" ItemsSource="{Binding SubItemNode}"  HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch" Height="300" Grid.RowSpan="6" Width="300"                                                                        dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True" dd:DragDrop.DropHandler="{Binding}" dd:DragDrop.UseDefaultDragAdorner="True">
<TreeView.Resources>
    <Style TargetType="{x:Type TreeView}">
        <Setter Property="local:CMTreeViewFilesBehavior.IsTreeViewFilesBehavior" Value="True"/>
    </Style>
    <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="IsSelected" Value="{Binding IsSelected}" />
        <Setter Property="local:CMTreeViewFilesItemBehavior.IsTreeViewFilesItemBehavior" Value="True"/>                              
    </Style>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
</TreeView.Resources>
    <TreeView.ContextMenu>
        <ContextMenu>
            <MenuItem Header="View File" Command="{Binding ExecuteFileCommand}" />

            <Separator />

            <MenuItem Header="Delete all" Command="{Binding DeleteAllFilesCommand}" />
            <MenuItem Header="Delete selected" Command="{Binding DeleteSelectedFilesCommand}" />
        </ContextMenu>
    </TreeView.ContextMenu>
    <TreeView.ItemTemplate>
      <HierarchicalDataTemplate ItemsSource="{Binding SubItemNode}" >
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>                             
                <Image Grid.Column="0" Margin="2" Width="32" Height="18" Source="{Binding Path=Icon}" HorizontalAlignment="Left" VerticalAlignment="Center" />
            <TextBlock Text="{Binding Path=Name}" Grid.Column="1" Margin="2"  VerticalAlignment="Center" Foreground="{Binding Path=Status, Converter={StaticResource ItemFileStatusToColor}}" FontWeight="{Binding Path=IsSelected, Converter={StaticResource BoolToFontWidth}}"/>
            </Grid>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

Am I doing somthing wrong? and why in .net 4.5 it works well ?

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf