2010-02-26 80 views
4

對不起,我的英文不好,那不是我的母語。用LINQ XML綁定組合框

我是一個初學者(自3天以來)與WPF和LINQ,和一個臨時用戶的C#。

昨天,我一直試圖解決我的問題,並閱讀了幾個文檔,但我的代碼中的一個錯誤仍然存​​在。

我傳遞的XElement到誰約束其內容的控制,但我一個,但在ComboBox

這裏是的XElement的XML:

<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> 

這裏是背後的代碼我的控制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.ObjectModel; 
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); 
      } 
     } 

    } 
} 

這裏是MonControle.xaml的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> 

我的問題是:

  • 當控制負載,這是確定的,但 如果我在 左側列表框切換愨元素,組合框 變化的價值...一個嘗試很多事情,做了很多 的測試,但不可能修復它!

  • 不可能在列表中輸入不是 的值,但是我想要 能夠做到這一點。同時,我做很多 測試,但我解決不了這個問題

  • 最後但並非最不重要:我想上升時的ObservableCollection 發生變化的 事件編寫的XML文件, 但不可能趕上一個事件... 我試過類似 xElementsObservable.CollectionChanged + = new NotifyCollectionChangedEventHandler(XElementsObservable_CollectionChanged); 但它不起作用...

感謝您的幫助!

回答

3

這很難,但我有解決所有我的問題!

這裏是解決方案:

在一個拳頭的時候,組合框XAML必須看起來像:

<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}" 
      /> 

這個回答問題1和2:因爲我們專注於屬性的值「我們可以編寫我們想要的,即使我們寫的值不在集合中,並且,ComboBox文本中的節點顯示問題消失了!

對於問題3,我的錯誤是我專注於可觀察的集合!

但是,解決方案很簡單,我在XDocument上附加了一個「Changed」事件,其中包含我在此處操作的所有XElements!

於是,我把這個代碼在我的軟件主窗口:

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瞧!

對不起,我的英語不好,我希望這會有所幫助...