2012-10-16 157 views
0

我有一個可由用戶編輯的組合框,所以我將Text屬性綁定到我的類的屬性。該同一個組合框的ItemsSource綁定到一個AsyncObservableCollection(我基於其他帖子,它很好地工作)。更新項目時出現問題源

但是,更新ItemsSource時出現問題。

下面是步驟來重現:

  1. 選擇在組合框中的值降下來了。
  2. 在組合框中鍵入一些文本。 (比如「aaa」)
  3. 更新ItemsSource。 (通過我的按鈕單擊)

結果:MyText屬性仍然設置爲您在(「aaa」)中鍵入的文本,但組合框顯示空白條目。

但是,如果您執行上述相同步驟但跳過步驟1,則組合框將正確顯示MyText屬性中的文本。這導致我相信在更新ItemsSource完成後,選定的索引/選定值正用於更新組合框。

關於如何在更新ItemsSource後保持顯示值與MyText屬性同步的任何想法?

在下面提供的代碼中,我正在更新按鈕上的ItemsSource,以便重現。

謝謝!

XAML:

<Window x:Class="WpfApplication2.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    DataContext="{Binding RelativeSource={RelativeSource Self}}" 
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded"> 
<Grid> 
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="comboBox1" VerticalAlignment="Top" Width="200" IsEditable="True" 
          DataContext="{Binding Path=MyDataClass}" 
          ItemsSource="{Binding Path=MyListOptions}" 
          SelectedIndex="{Binding Path=MySelectedIndex}" 
          Text="{Binding Path=MyText, UpdateSourceTrigger=LostFocus}" 
       > 
    </ComboBox> 
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="416,276,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> 
</Grid> 

後面的代碼:

using System.Collections.Generic; 
using System.ComponentModel; 
using System.Windows; 
using System.Diagnostics; 

namespace WpfApplication2 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public class DataClass : INotifyPropertyChanged 
     { 
      private string mytext = ""; 
      public string MyText 
      { 
       get 
       { 
        return mytext; 
       } 
       set 
       { 
        mytext = value; 
        OnPropertyChanged("MyText"); 
       } 
      } 

      private int myselectedindex = -1; 
      public int MySelectedIndex 
      { 
       get 
       { 
        return myselectedindex; 
       } 

       set 
       { 
        if (value != -1) 
        { 
         mytext = MyListOptions[value]; 
         OnPropertyChanged("MyText"); 
        } 
       } 
      } 

      private AsyncObservableCollection<string> mylistOptions = new AsyncObservableCollection<string>(); 
      public AsyncObservableCollection<string> MyListOptions 
      { 
       get 
       { 
        return mylistOptions; 
       } 

       set 
       { 
        mylistOptions.Clear(); 
        OnPropertyChanged("MyListOptions"); 
        foreach (string opt in value) 
        { 
         mylistOptions.Add(opt); 
        } 
        OnPropertyChanged("MyListOptions"); 
       } 
      } 

      public DataClass() 
      { 
      } 

      public event PropertyChangedEventHandler PropertyChanged; 

      internal void OnPropertyChanged(string prop) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(prop)); 
      } 
     } 

     public DataClass MyDataClass { get; set; } 

     public MainWindow() 
     { 
      MyDataClass = new DataClass(); 

      MyDataClass.MyListOptions.Add("Option 1 - Provides helpful stuff."); 
      MyDataClass.MyListOptions.Add("Option 2 - Provides more helpful stuff."); 
      MyDataClass.MyListOptions.Add("Option 3 - Provides extra helpful stuff."); 

      InitializeComponent(); 
     } 

     private void Window_Loaded(object sender, RoutedEventArgs e) 
     { 
     } 

     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      AsyncObservableCollection<string> newList = new AsyncObservableCollection<string>(); 
      newList.Add("Option A - Provides helpful stuff."); 
      newList.Add("Option B - Provides more helpful stuff."); 
      newList.Add("Option C - Provides extra helpful stuff."); 

      MyDataClass.MyListOptions = newList; 

     } 
    } 
} 

回答

0

好吧,我用的SelectedValue綁定到相同的屬性爲文本並設置其模式爲單向解決了這個問題。

<ComboBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="comboBox1" VerticalAlignment="Top" Width="200" IsEditable="True" 
          DataContext="{Binding Path=MyDataClass}" 
          ItemsSource="{Binding Path=MyListOptions}" 
          SelectedIndex="{Binding Path=MySelectedIndex}" 
          SelectedValue="{Binding Path=MyText, Mode=OneWay}" 
          Text="{Binding Path=MyText, UpdateSourceTrigger=LostFocus}"