0

我正在做这个小程序只是为了学习如何将一些数据从对象列表绑定到组合框。我想要做的是在文本块中显示组合框中某些单词的翻译。在组合框中,我想要英文单词,例如在 textblock 西班牙文中。

为此,我在 xaml 中创建了一个名为cmbBox1的组合框和一个名为tb1的文本块。

然后我创建了“单词”类:

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

    }

以及三个单词的列表:

 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("You selected the word" + word1.english),那就太好了。

这一切的目的是学习如何做一些更复杂的事情:我将加载一个带有一些数据通道的文本文件,每个通道都会有一堆属性,我希望能够选择通道然后绘制它. 非常感谢。

4

1 回答 1

2

我认为您应该逐步开始学习 C# 和 WPF。尝试绑定到类而不是绑定到实例或集合不是理解 WPF 的问题。

但是我知道有几个人在理解 WPF 中的数据绑定时遇到了很大的问题,所以我会拿起你的帖子来描述这个简单的例子。

1) 不要手动添加项目 ( cmbBox1.Items.Add(myword);) - 这就是数据绑定的用途。

2)您不能绑定到类型(如Binding Path=word)。您必须绑定到一个对象/集合。对象/集合必须是一个属性,而不是像您的实现那样的本地集合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>

完整的 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 + "\"");
    }
}
于 2013-05-11T11:26:51.823 回答