有没有一种方法可以防止或从ListViewItems中恢复为MS.Internal.NamedObject

时间:2019-05-20 17:01:28

标签: c# wpf

我有一个UserControl,它是一个以Canvas作为ItemsPanel的ListView。 ListView的ItemsSource绑定到CollectionViewSource,其Source属性绑定到ObservableCollection 。 DeviceItem是一个自定义类,除其他事项外,该类保留ListViewItem / DeviceItem的X / Y坐标,当用户希望保存其工作时会对其进行序列化。在PreviewMouseDown和PreviewTouchDown上,我检查正在单击/触摸的ListViewItem,以确保ListViewItem的内容是DeviceItem,以便当用户在屏幕上移动它时,软件可以更新DeviceItem上的坐标。这在99%的时间内都有效,但是在用户从集合中删除项目然后添加新项目之后,有时会遇到问题。删除/添加后,当用户单击/触摸ListViewItem时,其Content类型为MS.Internal.NamedObject,在这种情况下,我无法对该项目执行操作。我确定值得一提的是,用户正在另一个窗口中的单独控件中添加/删除该控件,该另一个窗口绑定到我们数据模型中的同一集合(ObservableCollection )。

当前,发生这种情况时,用户必须关闭并重新打开软件。我试图找到一种无需重启软件即可恢复的方法。

<UserControl.Resources>
    <Style x:Key="CanvasStyle" TargetType="{x:Type Canvas}">
        <EventSetter Event="Loaded" Handler="Canvas_Loaded"/>
    </Style>
    <CollectionViewSource Source="{Binding DiagramCollection}" x:Key="radioPacksDataView" />
</UserControl.Resources>
<Grid>
    <ListView Name="listBoxDevices"
              Background="Transparent"
              BorderThickness="0"
              Stylus.IsPressAndHoldEnabled ="False"
              ItemsSource="{Binding Source={StaticResource radioPacksDataView}}"
              PreviewMouseMove="ListBox_PreviewMouseMove"
              PreviewMouseDown="ListBox_PreviewMouseDown"
              PreviewMouseUp="ListBox_PreviewMouseUp"
              PreviewTouchDown="ListBox_PreviewTouchDown"
              PreviewTouchMove="ListBox_PreviewTouchMove"
              PreviewTouchUp="ListBox_PreviewTouchUp"
              SnapsToDevicePixels="True"                  
              IsHitTestVisible="{Binding AccessRights, ConverterParameter=Unlocked, Converter={StaticResource enumBooleanConverter}, Source={x:Static appData:Globals.CurrentUser}}"
              VirtualizingPanel.IsVirtualizing="False"
              IsSynchronizedWithCurrentItem="False">
        <ListView.Style>
            <Style>
                <Setter Property="ItemsControl.ItemsPanel">
                    <Setter.Value>
                        <ItemsPanelTemplate>
                            <Canvas Style="{DynamicResource CanvasStyle}"/>
                        </ItemsPanelTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=window, Path=IconStyle}" Value="0">
                        <Setter Property="ListView.ItemContainerStyle" Value="{DynamicResource MultiDeviceItemContainerStyle}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ElementName=window, Path=IconStyle}" Value="1">
                        <Setter Property="ListView.ItemContainerStyle" Value="{DynamicResource MultiDeviceItemContainerStyleV2}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListView.Style>
        <ListView.LayoutTransform>
            <ScaleTransform CenterX="0"
                            CenterY="0"
                            ScaleX="{Binding ZoomScale}"
                            ScaleY="{Binding ZoomScale}"/>
        </ListView.LayoutTransform>
    </ListView>
</Grid>

然后在后面的代码中

private void ListBox_PreviewMouseDown(object sender, MouseEventArgs e)
{
    if (m_selectedListViewItem == null)
    {
        m_selectedListViewItem = ItemsControl.ContainerFromElement((ListBox)sender, e.OriginalSource as DependencyObject) as ListViewItem;
        //this is the section that fails after removing/adding items, object is typeof(MS.Internal.NamedObject)
        if (m_selectedListViewItem.Content.GetType().IsSubclassOf(typeof(DeviceItem))) 
        {
            m_selectedDeviceItem = m_selectedListViewItem.Content as DeviceItem;
        }
    }
}

0 个答案:

没有答案