Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding ComboBox with LINQ XML
    primarykey
    data
    text
    <p>Excuse me for my poor English, that is not my native language.</p> <p>I'm a beginner (since 3 days) with WPF and LINQ, and a casual user of C#.</p> <p>Yesterday, i've trying all the day to solve my probleme and read several documentation, but a bug in my code persist.</p> <p>I pass a XElement to a control who bind its content, but i've a but in the ComboBox</p> <p>Here is the XML of the XElement :</p> <pre><code>&lt;racine&gt; &lt;element nom="Element 1"&gt; &lt;rubrique nom="Element 1 - rubrique 1"&gt; &lt;etat valeur="Hors service"&gt; &lt;option valeur="En service" /&gt; &lt;option valeur="Hors service service" /&gt; &lt;/etat&gt; &lt;observation&gt;lorem ipsum&lt;/observation&gt; &lt;/rubrique&gt; &lt;rubrique nom="Element 1 - rubrique 2"&gt; &lt;etat&gt; &lt;/etat&gt; &lt;observation&gt;titi toto&lt;/observation&gt; &lt;/rubrique&gt; &lt;/element&gt; &lt;element nom="Element 2"&gt; &lt;rubrique nom="Element 2 - rubrique 1"&gt; &lt;etat valeur="foo"&gt; &lt;/etat&gt; &lt;observation&gt;youpi&lt;/observation&gt; &lt;/rubrique&gt; &lt;rubrique nom="Element 2 - rubrique 2"&gt; &lt;etat valeur="bar"&gt; &lt;option valeur="En service" /&gt; &lt;/etat&gt; &lt;observation&gt;&lt;/observation&gt; &lt;/rubrique&gt; &lt;/element&gt; &lt;/racine&gt; </code></pre> <p>Here is the code behind of my control MonControle.xaml.cs :</p> <pre><code>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.ObjectModel; using System.Collections.Specialized; namespace MonProjet.Controles { /// &lt;summary&gt; /// Logique d'interaction pour MonControle.xaml /// &lt;/summary&gt; public partial class MonControle : UserControl { XElement xRacine; ObservableCollection&lt;XElement&gt; xElementsObservable = new ObservableCollection&lt;XElement&gt;(); 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); } } } } </code></pre> <p>And here is the xaml for MonControle.xaml :</p> <pre><code>&lt;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"&gt; &lt;!-- 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 --&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="25*"/&gt; &lt;ColumnDefinition Width="Auto" MinWidth="4"/&gt; &lt;ColumnDefinition Width="75*"/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;DockPanel Grid.Column="0" LastChildFill="True"&gt; &lt;ListBox Name="lbxElements" ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Attribute[nom].Value" /&gt; &lt;/DockPanel&gt; &lt;GridSplitter Grid.Column="1" ResizeBehavior="PreviousAndNext" Width="4" VerticalAlignment="Stretch"/&gt; &lt;DockPanel Grid.Column="2" LastChildFill="True" DataContext="{Binding Path=SelectedItem.Elements[rubrique], ElementName=lbxElements, UpdateSourceTrigger=PropertyChanged}"&gt; &lt;ListBox ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}" IsSynchronizedWithCurrentItem="True"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;GroupBox Header="{Binding Path=Attribute[nom].Value}"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;!-- http://stackoverflow.com/questions/561166/binding-wpf-combobox-to-a-custom-list --&gt; &lt;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}" /&gt; &lt;TextBox MinWidth="150" AcceptsReturn="False" AcceptsTab="False" TextWrapping="NoWrap" Text="{Binding Path=Element[observation].Value, UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;/StackPanel&gt; &lt;/GroupBox&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/DockPanel&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>My questions are :</p> <ul> <li><p>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 !</p></li> <li><p>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</p></li> <li><p>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...</p></li> </ul> <p>Thanks by advance for your help !</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload