如何显示从文本框的值和从组合框到ListView列的SelectedItem

时间:2019-12-15 20:50:51

标签: c# wpf mvvm

enter image description here我想从组合框到列表视图列中显示所选CourseName和CourseiD的值 我可以显示年龄和加入日期。请提出我需要做的更改。 我正在从CombobBox中将选定项目的值放入ViewModel中的不同属性“ SelectedCourseName”和“ SelectedCourseId”中。 请帮助以及如何获取学生可观察集合中的值,因为它是我的ListView的ItemSource。enter image description here

学生班(模型班)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MVVMDemo
{
    public class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Course { get; set; }
        public DateTime JoiningDate { get; set; }
        public string CourseName { get; set; }
        public string CourseID { get; set; }
    }
}

MainWindow.Xaml

<Window x:Class="MVVMDemo.UserRegistrationView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:viewmodel="clr-namespace:MVVMDemo"
    Title="Window1" Height="300" Width="575.851">
    <Window.Resources>
        <viewmodel:ViewModel x:Key="ViewModel"/>
        <viewmodel:DatetimeToDateConverter x:Key="MyConverter"/>
    </Window.Resources>
    <Grid DataContext="{Binding Source={StaticResource ViewModel}}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Row="0" Grid.Column="0" Text="Name" HorizontalAlignment="Center"/>
        <TextBox Grid.Row="0" Grid.Column="1" Width="100" HorizontalAlignment="Center" Text="{Binding Student.Name, Mode=TwoWay}" Margin="76,0"/>
        <TextBlock Grid.Row="1" Grid.Column="0" Text="Age" HorizontalAlignment="Center"/>
        <TextBox Grid.Row="1" Grid.Column="1" Width="100" HorizontalAlignment="Center" Text="{Binding Student.Age, Mode=TwoWay}"/>
        <Button Content="Submit" Command="{Binding SubmitCommand}" HorizontalAlignment="Right" Grid.Row="2" Grid.Column="0"/>

        <ComboBox Grid.Column="1" ItemsSource="{Binding FillCourseId}"  Name="cmb_CourseIDName" HorizontalAlignment="Left" Margin="164.191,5,0,0" 
                  Grid.Row="3" VerticalAlignment="Top" Width="168.342" Grid.RowSpan="2" DataContext="{Binding Source={StaticResource ViewModel}}"  FontSize="8" IsReadOnly="True" 
                  SelectedValue="{Binding SelectedCourseIdName,Mode=TwoWay}" RenderTransformOrigin="1.431,0.77">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" >
                        <TextBlock Text="{Binding Path=CourseName, Mode=TwoWay}"/>
                        <TextBlock Text=" - "/>
                        <TextBlock Text="{Binding Path=CourseID, Mode=TwoWay}"/>
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>

        </ComboBox>
        <ListView ItemsSource="{Binding Students}" Grid.Row="4" Grid.Column="1" Margin="62.551,54.019,78.762,76.829"
                  ScrollViewer.CanContentScroll="False" 
                 ScrollViewer.VerticalScrollBarVisibility="Visible">
            <ListView.View >
                <GridView  >
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="60"/>
                    <GridViewColumn  Header="Age" DisplayMemberBinding="{Binding Age}" Width="60"/>
                    <GridViewColumn  Header="Joining Date" DisplayMemberBinding="{Binding JoiningDate, Converter={StaticResource MyConverter}}" Width="80" />
                    <GridViewColumn Header="Course Name" DisplayMemberBinding="{Binding SelectedCourseName}" Width="80"/>
                    <GridViewColumn Header="CourseId" DisplayMemberBinding="{Binding SelectedCourseId}" Width="60"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>

</Window>

ViewModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Data;
using System.ComponentModel;

namespace MVVMDemo
{
    public class ViewModel : ViewModelBase
    {
        static String connectionString = @"Data Source=RaMCO-PC\SQLEXPRESS;Initial Catalog=SIT_Ramco_DB;Integrated Security=True;";
        SqlConnection con;
        SqlCommand cmd;
        private Student _student;
        private ObservableCollection<Student> _students;
        private ICommand _SubmitCommand;

        public Student Student
        {
            get
            {
                return _student;
            }
            set
            {
                _student = value;
                NotifyPropertyChanged("Student");
            }
        }
        private ObservableCollection<Student> _fillCourseId = new ObservableCollection<Student>();
        public ObservableCollection<Student> FillCourseId
        {
            get { return _fillCourseId; }
            set
            {
                _fillCourseId = value;
                OnPropertyChanged("SystemStatusData");
            }
        }
        public ObservableCollection<Student> Students
        {
            get
            {
                return _students;
            }
            set
            {
                _students = value;
                NotifyPropertyChanged("Students");
            }
        }

        private Student _selectedcourseIdname;

        public Student SelectedCourseIdName
        {
            get { return _selectedcourseIdname; }
            set
            {
                _selectedcourseIdname = value;
                OnPropertyChanged("SelectedCourseIdName");

            }

        }
        public string SelectedCourseId
        {
            get { return _selectedcourseIdname.CourseID; }
            set
            {
                _selectedcourseIdname.CourseID = value;
                OnPropertyChanged("SelectedCourseId");

            }

        }


        public string SelectedCourseName
        {
            get { return _selectedcourseIdname.CourseName; }
            set
            {
                _selectedcourseIdname.CourseName = value;
                OnPropertyChanged("SelectedCourseName");

            }

        }

        public ICommand SubmitCommand
        {
            get
            {
                if (_SubmitCommand == null)
                {
                    _SubmitCommand = new RelayCommand(param => this.Submit(),
                        null);
                }
                return _SubmitCommand;
            }
        }

        //********************************************* Functions*******************************************// 

        public void GetCourseIdFromDB()
        {
            try
            {
                con = new SqlConnection(connectionString);
                con.Open();
                cmd = new SqlCommand("select * from dev_Course", con);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                // Student Student = new Student();

                for (int i = 0; i < dt.Rows.Count; ++i)
                    FillCourseId.Add(new Student
                    {
                        CourseID = dt.Rows[i][0].ToString(),
                        CourseName = dt.Rows[i][1].ToString()
                    });

            }
            catch (Exception ex)
            {

            }

        }

        public ViewModel()
        {
            Student = new Student();
            Students = new ObservableCollection<Student>();
            Students.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Students_CollectionChanged);
            GetCourseIdFromDB();
        }

        void Students_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            NotifyPropertyChanged("Students");
        }

        private void Submit()
        {
            Student.JoiningDate = DateTime.Today.Date;
            Students.Add(Student);
            Student = new Student();
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyname)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }
    }
}

MainWindow.Xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Data;
using System.ComponentModel;

namespace MVVMDemo
{
    public class ViewModel : ViewModelBase
    {
        static String connectionString = @"Data Source=RITESH-PC\SQLEXPRESS;Initial Catalog=SIT_Ritesh_DB;Integrated Security=True;";
        SqlConnection con;
        SqlCommand cmd;
        private Student _student;
        private ObservableCollection<Student> _students;
        private ICommand _SubmitCommand;

        public Student Student
        {
            get
            {
                return _student;
            }
            set
            {
                _student = value;
                NotifyPropertyChanged("Student");
            }
        }
        private ObservableCollection<Student> _fillCourseId = new ObservableCollection<Student>();
        public ObservableCollection<Student> FillCourseId
        {
            get { return _fillCourseId; }
            set
            {
                _fillCourseId = value;
                OnPropertyChanged("SystemStatusData");
            }
        }
        public ObservableCollection<Student> Students
        {
            get
            {
                return _students;
            }
            set
            {
                _students = value;
                NotifyPropertyChanged("Students");
            }
        }

        private Student _selectedcourseIdname;

        public Student SelectedCourseIdName
        {
            get { return _selectedcourseIdname; }
            set
            {
                _selectedcourseIdname = value;
                OnPropertyChanged("SelectedCourseIdName");

            }

        }
        public string SelectedCourseId
        {
            get { return _selectedcourseIdname.CourseID; }
            set
            {
                _selectedcourseIdname.CourseID = value;
                OnPropertyChanged("SelectedCourseId");

            }

        }


        public string SelectedCourseName
        {
            get { return _selectedcourseIdname.CourseName; }
            set
            {
                _selectedcourseIdname.CourseName = value;
                OnPropertyChanged("SelectedCourseName");

            }

        }

        public ICommand SubmitCommand
        {
            get
            {
                if (_SubmitCommand == null)
                {
                    _SubmitCommand = new RelayCommand(param => this.Submit(),
                        null);
                }
                return _SubmitCommand;
            }
        }

        //********************************************* Functions*******************************************// 

        public void GetCourseIdFromDB()
        {
            try
            {
                con = new SqlConnection(connectionString);
                con.Open();
                cmd = new SqlCommand("select * from dev_Course", con);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                // Student Student = new Student();

                for (int i = 0; i < dt.Rows.Count; ++i)
                    FillCourseId.Add(new Student
                    {
                        CourseID = dt.Rows[i][0].ToString(),
                        CourseName = dt.Rows[i][1].ToString()
                    });

            }
            catch (Exception ex)
            {

            }

        }

        public ViewModel()
        {
            Student = new Student();
            Students = new ObservableCollection<Student>();
            Students.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Students_CollectionChanged);
            GetCourseIdFromDB();
        }

        void Students_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            NotifyPropertyChanged("Students");
        }

        private void Submit()
        {
            Student.JoiningDate = DateTime.Today.Date;
            Students.Add(Student);
            Student = new Student();
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyname)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }
    }
}

0 个答案:

没有答案
相关问题