【问题标题】:WPF fill in textbox based on selected item comboboxWPF根据所选项目组合框填充文本框
【发布时间】:2017-01-08 04:14:04
【问题描述】:

我制作了一个关于电影和演员的 WPF 项目(编程新手)。

现在我可以通过输入他的姓名、国家、生日等手动创建一个新演员(链接到电影)。由于我要添加越来越多的数据,我希望可以选择现有的来自组合框中的演员,然后他的姓名、国家、生日等会自动填写在我提供的文本框中,您通常会在其中手动添加新信息。

我的演员有一个演员 ID、名字、姓氏、国家和出生日期。 如果我想创建一个新演员,我只需填写这些内容并单击保存,它就会创建一个新演员。保存的东西现在并不重要。

在 Actor.cs 中我声明了这些:

public class Actor
{
    public int ActorID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    //Etc.
}

然后这些被称为 act.FirstName 等

我的组合框被称为:comboBoxExistingActors,它的 Itemsource 是:

        comboBoxExistingActors.ItemsSource = ActorRepository.ActorList();

这个 ActorList 在我的 ActorRepository 中定义:

    public static List<Actor> ActorList()
    {
        string command = "SELECT DISTINCT FirstName, LastName FROM tblActors ORDER BY tblActors.LastName";
        OleDbDataAdapter adapter = new OleDbDataAdapter(command, connectionString);
        DataTable datatable = new DataTable();
        adapter.Fill(datatable);

        List<Actor> lijst = new List<Actor>();

        for (int i = 0; i < datatable.Rows.Count; i++)
        {
            Actor act = new Actor();

            act.FirstName = datatable.Rows[i].Field<string>("FirstName");
            act.LastName = datatable.Rows[i].Field<string>("LastName");

            lijst.Add(act);
        }
        return lijst;
    }

现在我希望当我从此组合框中选择演员时,我的文本框会填写演员的详细信息:

    private void comboBoxExistingActors_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        textBoxFirstName.Text = ???
        //textBoxLastname etc.
    }

我不确定它是否这么简单,但我需要一种方法将我的 act.Firstname 从所选演员中获取到 textBoxFirstName 中。 我希望我已经提供了足够的信息来理解我的问题,如果没有,请说出来,我会为您提供。

提前致谢!

【问题讨论】:

    标签: c# wpf combobox textbox


    【解决方案1】:

    我认为您正在寻找这样的东西:

    private void comboBoxExistingActors_SelectionChanged(object sender, SelectionChangedEventArgs e)
     {
        textBoxFirstName.Text = ((Actor)comboBoxExistingActors.SelectedItem).FirstName;
     }
    

    【讨论】:

      【解决方案2】:

      使用以下代码:

      private ListCollectionView view; 
      ICollection<Actor> actors= GetActors();
      this.DataContext = actors;
      view = (ListCollectionView)CollectionViewSource.GetDefaultView(this.DataContext);
      

      在xml中:

      <ComboBox Name="lstActors" DisplayMemberPath="FirstName"
      Text="{Binding Path=FirstName}"
      SelectionChanged="lstActors_SelectionChanged"></ComboBox>
      
      <TextBox Text="{Binding Path=FistName}"/>
      <TextBox Text="{Binding Path=LastName}"/>
      <TextBox Text="{Binding Path=ActorID}"/>
      

      在 SelectionChanged 事件中:

      private void lstActors_SelectionChanged(object sender, RoutedEventArgs e)
      {
      view.MoveCurrentTo(lstActors.SelectedItem);
      }
      

      更简单的解决方案是将 ItemsControl.IsSynchronizedWithCurrentItem 设置为 true。这样一来, 当前选择的项目会自动同步以匹配视图的当前位置,没有 需要代码。

      【讨论】:

        【解决方案3】:

        推荐的实现方式是使用 MVVM 设计模式:https://msdn.microsoft.com/en-us/library/hh848246.aspx

        创建一个视图模型类:

        public class ActorViewModel : INotifyPropertyChanged
        {
            public ActorViewModel()
            {
                Actors = ActorRepository.ActorList();
            }
        
            public List<Actor> Actors { get; private set; }
        
            private Actor _selectedActor;
            public Actor SelectedActor
            {
                get { return _selectedActor; }
                set { _selectedActor = value; NotifyPropertyChanged(); }
            }
        
        
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
        

        ...并将窗口的 DataContext 设置为此一个实例:

        public MainWindow()
        {
            InitializeComponent();
            DataContext = new ActorViewModel();
        }
        

        然后您可以绑定到它的任何属性:

        <ComboBox ItemsSource="{Binding Actors}" DisplayMemberPath="FirstName"
                          SelectedItem="{Binding SelectedActor}" />
        
        <TextBox Text="{Binding SelectedActor.FirstName}" />
        <TextBox Text="{Binding SelectedActor.LastName}" />
        

        如果视图模型类实现了 INotifyPropertyChanged 接口并在设置器中引发 PropertyChanged 事件,视图模型的 SelectedActor 属性将设置为您在组合框中选择的 Actor 对象,并且数据绑定的文本框将自动刷新SelectedActor 源属性。

        您也可以将文本框直接绑定到组合框:

        <ComboBox x:Name="cmb" ItemsSource="{Binding Actors}" DisplayMemberPath="FirstName"/>
        
        <TextBox Text="{Binding SelectedItem.FirstName, ElementName=cmb}" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多