c#WPF 一个窗口中两个TreeView,选择A的节点①,再选B的节点②,再选A的节点①时,SelectedItemChanged不能获取到第三次的Ayi

简称treeA

<TreeView Grid.Row="3" Name="deviceTreeFromCloud" SelectedItemChanged="deviceTreeFromCloud_SelectedItemChanged">
                    <TreeView.ItemContainerStyle>
                        <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource MetroTreeViewItem}">
                            <EventSetter Event="TreeViewItem.PreviewMouseRightButtonDown" Handler="deviceTreeFromCloud_MouseRight_Click"/>
                        </Style>
                    </TreeView.ItemContainerStyle>
                    <TreeView.Resources>
                        <ContextMenu  x:Key="OwnerTreeContextMenu" StaysOpen="true" Width="200">
                            <MenuItem Header="删除此用户及其设备" Click="deleteThisUser_Click" />
                        </ContextMenu>
                        <ContextMenu  x:Key="FacoryTreeContextMenu" StaysOpen="true" Width="200">
                            <MenuItem Header="删除此厂家及其设备" Click="newButtonForFactory_Click" />
                        </ContextMenu>
                        <ContextMenu  x:Key="FunctionTreeContextMenu" StaysOpen="true" Width="200">
                            <MenuItem Header="删除此分类及其设备" Click="newButtonForFunction_Click" />
                        </ContextMenu>
                    </TreeView.Resources>
                    <TreeView.ItemTemplate>
                        <HierarchicalDataTemplate ItemsSource="{Binding Path=ChildNodes}">
                            <StackPanel>
                                <!--<Label Content="{Binding Path=NodeName}" IsHitTestVisible ="False"/>-->
                                <TextBlock Text="{Binding Path=NodeName}" />
                            </StackPanel>
                        </HierarchicalDataTemplate>
                    </TreeView.ItemTemplate>
                </TreeView>

简称treeB

<TreeView Grid.Row="4" Name="deviceTreeBelongToLocal" SelectedItemChanged="deviceTreeBelongToLocal_SelectedItemChanged"> 
                    <TreeView.ItemContainerStyle>
                        <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource MetroTreeViewItem}">
                            <EventSetter Event="TreeViewItem.PreviewMouseRightButtonDown" Handler="deviceTreeBelongToLocal_MouseRight_Click"/>
                        </Style>
                    </TreeView.ItemContainerStyle>
                    <TreeView.ItemTemplate >
                        <HierarchicalDataTemplate ItemsSource="{Binding Path=ChildNodes}" >
                            <StackPanel >
                                <TextBlock Text="{Binding Path=NodeName}"   /> <!---->
                            </StackPanel>
                        </HierarchicalDataTemplate>
                    </TreeView.ItemTemplate>
                    <TreeView.Resources>
                        <ContextMenu  x:Key="FacoryTreeContextMenu" StaysOpen="true" Width="200">
                            <MenuItem Header="添加设备分类" Click="AddFacFunction_Click"/>
                            <MenuItem Header="删除厂家" Click="DeleteFactory_Click"/>
                            <MenuItem Header="修改" Click="ModifyFactory_Click"/>
                        </ContextMenu>
                        <ContextMenu  x:Key="FunctionTreeContextMenu" StaysOpen="true" Width="200">
                            <MenuItem Header="添加设备" Click="BtAdd_Click"/>
                            <MenuItem Header="粘贴设备" Click="BtPasteDev_Click"/>
                            <MenuItem Header="删除设备分类" Click="DeleteFacFunction_Click"/>
                            <MenuItem Header="修改" Click="ModifyFacFunction_Click"/>
                        </ContextMenu>
                    </TreeView.Resources>
                </TreeView>

treeA的SelectedItemChanged

private void deviceTreeFromCloud_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            BtAdd.Visibility = Visibility.Collapsed;
            BtDelete.Visibility = Visibility.Collapsed;
            BtSave.Visibility = Visibility.Collapsed;
            BtImport.Visibility = Visibility.Collapsed;

            DataGridDeviceList.Visibility = Visibility.Collapsed; 
            FromeCloud.Visibility = Visibility.Visible;

            if (sender != null && IsLoaded)
            {
                try
                {
                    var tvItem = (TreeViewItem)e.Source;
                    var itemCount = VisualTreeHelper.GetChildrenCount(tvItem);
                    for (var i = itemCount - 1; i >= 0; i--)
                    {
                        var child = VisualTreeHelper.GetChild(tvItem, i);
                        ((FrameworkElement)child).BringIntoView();
                    }
                }
                catch
                {

                }

                var tvitem = deviceTreeFromCloud.SelectedItem;
                //selectItem = tvitem; 
                TreeNode seleteNode = new TreeNode();
                seleteNode = tvitem as TreeNode;

                DataTable dtsource = null;
                if (seleteNode.thisIs == "user")
                {
                    TextBlockSelectItem.Text = "设备列表/" + seleteNode.NodeName;
                    dtsource = StaticDataSource.GetDeviceByOwner(seleteNode.NodeName);
                }
                else if (seleteNode.thisIs == "factory")
                {
                    TextBlockSelectItem.Text = "设备列表/" + seleteNode.whichOwner + "/" + seleteNode.NodeName;
                    dtsource = StaticDataSource.GetDeviceByOwnerAndFactory(seleteNode.whichOwner,seleteNode.NodeName);
                }
                else if (seleteNode.thisIs == "function")
                {
                    TextBlockSelectItem.Text = "设备列表/" + seleteNode.whichOwner + "/" + seleteNode.whichFactory + "/" + seleteNode.NodeName;
                    dtsource = StaticDataSource.GetDeviceByOwnerAndFactoryAndFunction(seleteNode.whichOwner, seleteNode.whichFactory, seleteNode.NodeName);
                }
                ModelHandler<Class_DeviceModel_List> classhelper = new ModelHandler<Class_DeviceModel_List> { };
                ObservableCollection<Class_DeviceModel_List> mydevicelist = classhelper.FillModel(dtsource);
                FromeCloud.ItemsSource = null;
                FromeCloud.ItemsSource = mydevicelist;
            }
        }

treeB的SelectedItemChangde

private void deviceTreeBelongToLocal_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {

            BtDelete.Visibility = Visibility.Visible;
            BtSave.Visibility = Visibility.Visible;
            BtImport.Visibility = Visibility.Visible;          

            DataGridDeviceList.Visibility = Visibility.Visible;
            FromeCloud.Visibility = Visibility.Collapsed;

            if (sender != null && IsLoaded)
            {
                var tvitem = deviceTreeBelongToLocal.SelectedItem;
                selectItem = tvitem;

                ModelHandler<Class_DeviceModel_List> classhelper = new ModelHandler<Class_DeviceModel_List> { };

                TreeNode seleteNode = new TreeNode();
                seleteNode = tvitem as TreeNode;


                DataTable dtsource = null;
                if (seleteNode.thisIs == "user")
                {
                    TextBlockSelectItem.Text = "设备列表/" + seleteNode.NodeName;
                    dtsource = StaticDataSource.GetDeviceByOwner(seleteNode.NodeName);
                    BtAdd.Visibility = Visibility.Collapsed;
                }
                else if (seleteNode.thisIs == "factory")
                {
                    TextBlockSelectItem.Text = "设备列表/" + seleteNode.whichOwner + "/" + seleteNode.NodeName;
                    dtsource = StaticDataSource.GetDeviceByOwnerAndFactory(seleteNode.whichOwner, seleteNode.NodeName);
                    BtAdd.Visibility = Visibility.Collapsed;
                }
                else if (seleteNode.thisIs == "function")
                {
                    TextBlockSelectItem.Text = "设备列表/" + seleteNode.whichOwner + "/" + seleteNode.whichFactory + "/" + seleteNode.NodeName;
                    dtsource = StaticDataSource.GetDeviceByOwnerAndFactoryAndFunction(seleteNode.whichOwner, seleteNode.whichFactory, seleteNode.NodeName);
                    BtAdd.Visibility = Visibility.Visible;
                }
                ObservableCollection<Class_DeviceModel_List> mydevicelist = classhelper.FillModel(dtsource);
                DataGridDeviceList.ItemsSource = null;
                DataGridDeviceList.ItemsSource = mydevicelist;
            }            
        }

当我选择A或B的节点时,应改显示节点的相关信息
图片说明
图片说明
图片说明

但是当我从图三再点回图二时,上边的信息是不变化的

zai_lu_shang__
zai_lu_shang__ 回复zai_lu_shang__: 刚写wpf没多久,就算有点儿思路也不知道怎么实现,还请各路大神指导
8 个月之前 回复
zai_lu_shang__
zai_lu_shang__ 回复zai_lu_shang__: 1、在我选treeB的节点时,让treeA恢复到没选过任何节点的状态? 2、设置焦点方面的东西?
8 个月之前 回复
zai_lu_shang__
zai_lu_shang__ 我知道是因为再选择treeA的时候,selectedchanged是没有变化的,所以根本不会触发SelectedItemChanged的方法,所以想了下解决思路,
8 个月之前 回复

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐