开发者

Binding ComboBox with LINQ XML

开发者 https://www.devze.com 2022-12-21 14:39 出处:网络
Excuse me for my poor English, that is not my native language. I\'m a beginner (since 3 days) with WPF and LINQ, and a casual user of C#.

Excuse me for my poor English, that is not my native language.

I'm a beginner (since 3 days) with WPF and LINQ, and a casual user of C#.

Yesterday, i've trying all the day to solve my probleme and read several documentation, but a bug in my code persist.

I pass a XElement to a control who bind its content, but i've a but in the ComboBox

Here is the XML of the XElement :

<racine>
    <element nom="Element 1">
      <rubrique nom="Element 1 - rubrique 1">
        <etat valeur="Hors service">
          <option valeur="En service" />
          <option valeur="Hors service service" />
        </etat>
        <observation>lorem ipsum</observation>
      </rubrique>
      <rubrique nom="Element 1 - rubrique 2">
        <etat>
        </etat>
        <observation>titi toto</observation>
      </rubrique>
    </element>
    <element nom="Element 2">
      <rubrique nom="Element 2 - rubrique 1">
        <etat valeur="foo">
        </etat>
        <observation>youpi</observation>
      </rubrique>
      <rubrique nom="Element 2 - rubrique 2">
        <etat valeur="bar">
          <option valeur="En service" />
        </etat>
        <observation></observation>
      </rubrique>
    </element>
</racine>

Here is the code behind of my control MonControle.xaml.cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.ComponentModel;
using System.Collections.Object开发者_开发技巧Model;
using System.Collections.Specialized;

namespace MonProjet.Controles
{
    /// <summary>
    /// Logique d'interaction pour MonControle.xaml
    /// </summary>
    public partial class MonControle : UserControl
    {
        XElement xRacine;

        ObservableCollection<XElement> xElementsObservable = new ObservableCollection<XElement>();

        public MonControle()
        {
            InitializeComponent();
            DataContext = xElementsObservable;
        }

        #region Propriété Attribus
        [Category("Configuration"), Browsable(false), Description("Element XML racine")]
        public XElement xRacine
        {
            get
            {
                return xRacine;
            }
            set
            {
                this.xRacine = value;
                MajXElementsObservable();

            }
        }
        #endregion

        private void MajXElementsObservable()
        {
            var requette = from xElements in xRacine.Descendants("element")
                           select (XElement)xElements;
            xElementsObservable.Clear();
            foreach (XElement xElement in requette)
            {
                xElementsObservable.Add(xElement);
            }
        }

    }
}

And here is the xaml for MonControle.xaml :

<UserControl x:Class="MonProjet.Controles.MonControle"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="Auto" Width="Auto">
    <!--
    http://www.youdev.net/post/2008/09/23/WPF-SplitContainer-2.aspx
    http://www.youdev.net/post/2009/03/19/WPF-SplitContainer-Part-2.aspx
    -->
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="25*"/>
            <ColumnDefinition Width="Auto" MinWidth="4"/>
            <ColumnDefinition Width="75*"/>
        </Grid.ColumnDefinitions>
        <DockPanel Grid.Column="0" LastChildFill="True">
            <ListBox Name="lbxElements" ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Attribute[nom].Value" />
        </DockPanel>
        <GridSplitter Grid.Column="1" ResizeBehavior="PreviousAndNext" Width="4" VerticalAlignment="Stretch"/>
        <DockPanel Grid.Column="2" LastChildFill="True" DataContext="{Binding Path=SelectedItem.Elements[rubrique], ElementName=lbxElements, UpdateSourceTrigger=PropertyChanged}">
            <ListBox ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}"
                     IsSynchronizedWithCurrentItem="True">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <GroupBox Header="{Binding Path=Attribute[nom].Value}">
                            <StackPanel Orientation="Horizontal">
                                <!-- http://stackoverflow.com/questions/561166/binding-wpf-combobox-to-a-custom-list -->
                                <ComboBox MinWidth="75" IsEditable="True"
                                          ItemsSource="{Binding Path=Element[etat].Elements[option], UpdateSourceTrigger=PropertyChanged}"
                                          DisplayMemberPath="Attribute[valeur].Value"
                                          SelectedValuePath="Attribute[valeur].Value" 
                                          SelectedValue="{Binding Path=Element[etat].Element[option].Attribute[valeur].Value}"
                                          />
                                <TextBox MinWidth="150" AcceptsReturn="False" AcceptsTab="False" TextWrapping="NoWrap"
                                         Text="{Binding Path=Element[observation].Value, UpdateSourceTrigger=PropertyChanged}" />
                            </StackPanel>
                        </GroupBox>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </DockPanel>
    </Grid>
</UserControl>

My questions are :

  • When the control load, it's ok, but if I switch betwen element in the left ListBox, value of the ComboBox change... A try many thing, do a lot of test, but impossible to fix it !

  • Impossible to enter value who are not in the list of , but i want be able to do it. As well, I do many test, but I can't solve the problem

  • Last but not least : i want rise a event when the ObservableCollection is changed for write the XML file, but impossible to catch an event... I've tried something like xElementsObservable.CollectionChanged += new NotifyCollectionChangedEventHandler(XElementsObservable_CollectionChanged); but it do not work...

Thanks by advance for your help !


It was hard, but i have the solution to all of my questions !

here is the solution :

In a fist time, the ComboBox XAML must look like that :

<ComboBox MinWidth="75" IsEditable="True"
          IsSynchronizedWithCurrentItem="False"
          ItemsSource="{Binding Path=Element[etat].Elements[option]}"
          DisplayMemberPath="Attribute[valeur].Value"
          Text="{Binding Element[etat].Attribute[valeur].Value, UpdateSourceTrigger=PropertyChanged}"
          />

This answer question 1 and 2 : as we focus on the value of the attribute "valeur" of the node who contain , we can write what we want, even if the value we write isn't in the collection, and, the problem of node display in the text of ComboBox is gone !

For the question 3, my mistake is i focus on the observable collection !

But, the solution is simple, i attached a "Changed" event on the XDocument who contain all the XElements i manipulate here !

So, i put this code in the main window of my software :

  private void InitPerso()
    {

        xDoc = XDocument.Load(@"C:\fichier.xml");

        xDoc .Changed += new EventHandler<XObjectChangeEventArgs>(XDoc_Changed);

    }

    private void XEdls_Changed(object sender, XObjectChangeEventArgs e)
    {
        xDoc .Save(@"C:\fichier.xml");
    }

Et voilà !

Excuse me for my bad English, I hope this will help...

0

精彩评论

暂无评论...
验证码 换一张
取 消