j’ai un problème avec un Treeview relier au XmlDocument via XmlDataProvider
Lorsque il un XmlElement avec deux attributes et plus la suppression marche , mais lorsque il une seul attribute elle est supprimer dans le XmlDocument mais pas dans le treeview


Xaml Code :

Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
<XmlDataProvider   x:Key="xmlDataProvider_TreeView_Scene" ></XmlDataProvider>


Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
<DataTemplate x:Key="attributeTemplate">
 
            <DataGridCell >
                <StackPanel Name="SP1" Orientation="Horizontal" Margin="3,0,0,0" HorizontalAlignment="Center">
                    <TextBlock Text="{Binding Path=Name}" Foreground="{StaticResource xmAttributeBrush}" />
                    <TextBlock  Text="=&quot;" Foreground="{StaticResource xmlMarkBrush}"/>
                    <TextBlock Text="{Binding Path=Value}" Foreground="{StaticResource xmlValueBrush}" />
                    <TextBlock Text="&quot;" Foreground="{StaticResource xmlMarkBrush}"/>
                </StackPanel>
            </DataGridCell>
 
        </DataTemplate>


Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
<Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="True"/>
 
            <Style.Triggers>
                <Trigger Property="IsExpanded" Value="false">
                <Setter    Property="Panel.Background"   Value="{StaticResource SelectedItemAreaBrush}" />
                <Setter    Property="Border.BorderBrush"  Value="{StaticResource SelectedItemBorderBrush}" />
                <Setter    Property="TextElement.Foreground"    Value="{DynamicResource  {x:Static SystemColors.HighlightTextBrushKey}}" />
             </Trigger>
            </Style.Triggers>
 
        </Style>



Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<HierarchicalDataTemplate x:Key="Template_TreeView"  DataType="Scene">
 
            <StackPanel Orientation="Horizontal" Focusable="False">
                <TextBlock x:Name="tbName"  Foreground="{StaticResource xmlElementBrush}" />
                <TextBlock Text=" : " FontSize="15" Foreground="{StaticResource xmlElementBrush}" />
 
                <ItemsControl ItemTemplate="{StaticResource attributeTemplate}"  ItemsSource="{Binding Path=Attributes}" HorizontalAlignment="Center">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
 
                </ItemsControl>
            </StackPanel>
 
 
            <HierarchicalDataTemplate.ItemsSource>
                    <Binding XPath="child::node()" />
            </HierarchicalDataTemplate.ItemsSource>
 
 
            <HierarchicalDataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                    <Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name}"/>
                </DataTrigger>
            </HierarchicalDataTemplate.Triggers>
 
 
        </HierarchicalDataTemplate>


Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
<TreeView Name="treeViewScene" MouseDoubleClick="TreeViewScene_MouseDoubleClick"  >
 
 <TreeViewItem Name="TreeViewItem_Scene"  ItemsSource="{Binding Source={StaticResource xmlDataProvider_TreeView_Scene},  XPath=* }"  ItemTemplate="{StaticResource ResourceKey=Template_TreeView}"   
 </TreeViewItem>
 
</TreeView>


C# code :


Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if (this.xmlNodeSelectionner is XmlElement)
                {
                    this.xmlNodeSelectionner.ParentNode.RemoveChild(this.xmlNodeSelectionner);
                }
 
 
                else if (this.xmlNodeSelectionner is XmlAttribute )
                {
 
                    XmlAttribute xmlAttributeSelectionner = this.xmlNodeSelectionner as XmlAttribute;
 
                    XmlElement xmlElementParent = xmlAttributeSelectionner.OwnerElement;
 
                    xmlElementParent.Attributes.Remove(xmlAttributeSelectionner);
 
 
                }
 
 
                this.xmlDataProvider_TreeView_Scene.Refresh();
                this.xmlNodeSelectionner = null;