如何将我的数据绑定到我的组合框控件wpf?

时间:2017-08-21 10:58:53

标签: c# wpf

这是我想要绑定到ComboBox控件的数据

public partial class MainWindow : Window
{
    Contact contact = new Contact();


    public MainWindow()
    {
        Contact contact = new Contact();
        contact.Titles.Add(new Title { title = "Mr" });
        contact.Titles.Add(new Title { title = "Dr " });
        contact.Titles.Add(new Title { title = "Mis" });
        contact.Titles.Add(new Title { title = "Miss" });
        contact.Titles.Add(new Title { title = "Sir" });

        contact.Genders.Add(new Gender { gender = "Male" });
        contact.Genders.Add(new Gender { gender = "Female" });

        DataContext = contact.Genders;
        DataContext = contact.Titles;

    }
}

1 个答案:

答案 0 :(得分:1)

您不需要设置两次DataContext属性,因为它是您的MainWindow类的属性。 首先让Contact对象成为MainWindow的DataContext,这就是全部。 为它实现INotifyPropertyChanged。

并在XAML中进行绑定:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ComboBox ItemsSource="{Binding Path=Titles}" MaxHeight="20"/>
        <ComboBox ItemsSource="{Binding Path=Genders}" Grid.Column="1"  MaxHeight="20"/>
    </Grid>
</Window>

还有MainWindows.xaml.cs

:

 public partial class MainWindow : Window
    {
        public Contact contact;
        public MainWindow()
        {
            InitializeComponent();
            contact = new Contact();
            this.DataContext = contact;

            contact.Titles.Add(new Title { title = "Mr" });
            contact.Titles.Add(new Title { title = "Dr " });
            contact.Titles.Add(new Title { title = "Mis" });
            contact.Titles.Add(new Title { title = "Miss" });
            contact.Titles.Add(new Title { title = "Sir" });

            contact.Genders.Add(new Gender { gender = "Male" });
            contact.Genders.Add(new Gender { gender = "Female" });
        }
    }

    public class Contact : INotifyPropertyChanged
    {
        public ObservableCollection<Title> Titles
        {
            get { return _titles; }
            set
            {
                _titles = value;
                NotifyPropertyChanged(nameof(Titles));
            }
        }

        public ObservableCollection<Gender> Genders
        {
            get { return _genders; }
            set
            {
                _genders = value;
                NotifyPropertyChanged(nameof(Genders));
            }
        }

        private ObservableCollection<Title> _titles { get; set; }
        private ObservableCollection<Gender> _genders { get; set; }


        public Contact()
        {
            Titles = new ObservableCollection<Title>();
            Genders = new ObservableCollection<Gender>();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }

    public class Title
    {
        public string title { get; set; }
        public override string ToString()
        {
            return title;
        }
    }

    public class Gender
    {
        public string gender { get; set; }
        public override string ToString()
        {
            return gender;
        }
    }