繁体   English   中英

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

[英]How do I bind my data to my combobox control 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;

    }
}

您不需要设置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;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM