2013-05-11 88 views
0

我正在做這個小程序,只是爲了學習如何將一些對象的數據綁定到組合框。我想要做的是在文本塊中顯示組合框中某些單詞的翻譯。在組合框中,我想要英文單詞,例如在文本塊西班牙語中。Combobox WPF list

對於我創建XAML中稱爲cmbBox1組合框和文本塊稱爲TB1

然後,我創建的類 「詞」:

​​

和列表的三個詞:

private void Window_Loaded_1(object sender, RoutedEventArgs e) 
    { 
     // Creation of a list of objects of class "word" 
     List<word> mydictionary = new List<word>(); 

     word word1 = new word(); 
     word1.english = "Hello"; 
     word1.spanish = "Hola"; 

     word word2 = new word(); 
     word2.english = "Goodbye"; 
     word2.spanish = "Adios"; 

     word word3 = new word(); 
     word3.english = "How are you?"; 
     word3.spanish = "¿Qué tal?"; 

     mydictionary.Add(word1); 
     mydictionary.Add(word2); 
     mydictionary.Add(word3); 

     //Adding the objects of the list mydictionary to combobox <--- 
     foreach (word myword in mydictionary) 
     { 
      cmbBox1.Items.Add(myword); 
     } 



    } 

和INT XAML我已經爲我的組合框:

<ComboBox x:Name="cmbBox1" HorizontalAlignment="Left" Margin="133,122,0,0" VerticalAlignment="Top" Width="120" 
        ItemsSource="{Binding Path=word}" 
        DisplayMemberPath="english" 
        SelectedValuePath="english" 
       SelectedValue="{Binding Path=word}" /> 

我希望在組合框中顯示屬性「english」,並在文本塊中顯示屬性「spanish」。如果用戶在組合框中單擊一個單詞時會執行一個不同的方法,例如MessageBox.Show(「您選擇了單詞」+ word1.english「),這將會很不錯。

所有這些的目的是學習如何做更復雜的事情:我將加載一些帶有一些數據通道的文本文件,每個通道都會有一堆特性,我希望能夠選擇通道然後繪製它。非常感謝你。

回答

2

我認爲你應該開始學習C#和WPF一步一步。試圖綁定到類而不是綁定到實例或集合不是理解WPF的問題。

但是我知道有幾個人在WPF中理解數據綁定的大問題,所以我會拿起你的帖子來描述這個簡單的例子。

1)不要手動添加項目(cmbBox1.Items.Add(myword);) - 這就是數據綁定的目的。

2)你不能綁定到一個類型(如Binding Path=word)。你必須綁定到一個objet /集合。對象/集合必須是屬性,而不是像您的實現mydictionary這樣的本地集合。

public List<Word> MyDictionary {get; set;} 
public Word SelectedWord {get; set;} 

ItemsSource="{Binding Path=MyDictionary}" 
SelectedItem="{Binding SelectedWord}" 

3)必須設置包含數據集合的DataContext的對象,你的情況:

DataContext = this; 

4)反應來選擇的變化,處理SelectionChanged事件:

<ComboBox [...] SelectionChanged="cmbBox1_SelectionChanged" /> 

完全WPF:

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded"> 
<Grid> 
    <ComboBox x:Name="cmbBox1" HorizontalAlignment="Left" Margin="133,122,0,0" 
       VerticalAlignment="Top" Width="120" 
       ItemsSource="{Binding MyDictionary}" 
       DisplayMemberPath="english" 
       SelectedValuePath="english" 
       SelectedItem="{Binding SelectedWord}" 
       SelectionChanged="cmbBox1_SelectionChanged" /> 
</Grid> 
</Window> 

Full C#:

public class Word 
{ 
    public string english { get; set; } 
    public string spanish { get; set; } 
} 

public partial class MainWindow : Window 
{ 
    public List<Word> MyDictionary {get; set;} 

    public Word SelectedWord {get; set;} 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     MyDictionary = new List<Word>(); 

     Word word1 = new Word(); 
     word1.english = "Hello"; 
     word1.spanish = "Hola"; 

     Word word2 = new Word(); 
     word2.english = "Goodbye"; 
     word2.spanish = "Adios"; 

     Word word3 = new Word(); 
     word3.english = "How are you?"; 
     word3.spanish = "¿Qué tal?"; 

     MyDictionary.Add(word1); 
     MyDictionary.Add(word2); 
     MyDictionary.Add(word3); 

     DataContext = this; 
    } 

    private void cmbBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     MessageBox.Show("You have selected the word \"" + SelectedWord.spanish + "\""); 
    } 
} 
+0

非常感謝。你幫了我很多。 – Sturm 2013-05-11 12:29:47