【问题标题】:WPF MVVM TreeView item source losing context after commandWPF MVVM TreeView 项目源在命令后丢失上下文
【发布时间】:2014-08-19 10:14:22
【问题描述】:

我有一个包含文件的树视图,每个视图模型都有一个项目源,它是一个带有文件项目的 ObservableCollection:

public ObservableCollection<CMItemFileNode> SubItemNode

在每个项目上,我都有上下文菜单选项(删除、执行..)。 如果我从一个视图模型移动到另一个视图模型的 ObservableCollection 文件正确更新并正确显示,但是,当我执行删除文件项之类的上下文菜单命令时,该命令执行良好,但是当我移动到另一个视图模型(其中包含的 SubItemNode ObservableCollection 是own) 命令执行后,WPF 仍然认为我在我所在的最后一个视图模型中,而不是我真正所在的那个。

非常重要的一点是,当我更新到 .net 4.5(不幸的是我不能这样做)时,一切正常,并且 ObservableCollection 解决了正确的视图模型。

这是树视图:

<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>

我做错了吗?为什么在 .net 4.5 中运行良好?

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    显然在 .net 4 中执行上下文菜单命令后,我们会在树中丢失上下文。 为了解决这个问题,我们需要将它添加到上下文菜单中:

       <ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}" >
    

    如果我们升级到 .net 4.5,那就没有必要了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 2013-03-09
      • 2011-04-19
      • 1970-01-01
      • 2020-05-08
      • 2013-10-08
      • 2016-11-20
      相关资源
      最近更新 更多