Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

c# - 如何在MVVM WPF C#中显示ComboBox中ObservaBleCollection的值(How to Display Values from ObservaBleCollection in ComboBox In MVVM WPF C#)

I am getting the values in my observable Collection in ViewModel Class but not getting the values displayed in my ComboBox please help I referrred this link https://www.c-sharpcorner.com/article/explain-combo-box-binding-in-mvvm-wpf/

(我在ViewModel类的可观察集合中获取值,但未在ComboBox中显示值,请帮助我引用了此链接https://www.c-sharpcorner.com/article/explain-combo-box-binding-in -mvvm-wpf /)

XAML

(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="300">
            <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="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="4" Grid.Column="0" RenderTransformOrigin="0.61,1.96" Margin="0,27.348,0,159.452"/>
                <ComboBox Grid.Column="1" ItemsSource="{Binding Path=FillCourseId}"  Name="cmb_CourseIDName" HorizontalAlignment="Left" Margin="76,5,0,0" 
                          Grid.Row="3" VerticalAlignment="Top" Width="120" Grid.RowSpan="2">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Path=CourseName}"/>
                                <TextBlock Text=" - "/>
                                <TextBlock Text="{Binding Path=CourseID}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>

                </ComboBox>

                <ListView ItemsSource="{Binding Students}" Grid.Row="4" Grid.Column="1" Width="200" 
                          Margin="27.657,34.948,24.342,159.452">
                    <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" />
                        </GridView>
                    </ListView.View>
                </ListView>

            </Grid>

        </Window>

ViewModel Class here iam getting my values in my observableCollection FillCourseId after referring the link i mentioned iam still not able to get the values.

(在引用我提到的链接后,我在ViewModel类中获取了我的值在我的observableCollection FillCourseId中,但仍然无法获取值。)

please help !!!

(请帮忙 !!!)


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


        namespace MVVMDemo
        {
            public class ViewModel : ViewModelBase, INotifyPropertyChanged
            {
                private Student _student;
                private ObservableCollection<Student> _students;
                private ICommand _SubmitCommand;
                private ObservableCollection<Student> _fillCourseId = new ObservableCollection<Student>();
                static String connectionString = @"Data Source=Ramco-PCSQLEXPRESS;Initial Catalog=SIT_Ramco_DB;Integrated Security=True;";
                SqlConnection con;
                SqlCommand cmd;
               // SqlDataAdapter adapter;
               // DataSet ds;
                //SqlDataReader reader;

                public ObservableCollection<Student> FillCourseId
                {
                    get { return _fillCourseId; }
                    set
                    {
                        _fillCourseId = value;
                        OnPropertyChanged("SystemStatusData");
                    }
                }


                public Student Student
                {
                    get
                    {
                        return _student;
                    }
                    set
                    {
                        _student = value;
                        NotifyPropertyChanged("Student");
                    }
                }
                public ObservableCollection<Student> Students
                {
                    get
                    {
                        return _students;
                    }
                    set
                    {
                        _students = value;
                        NotifyPropertyChanged("Students");
                    }
                }

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



                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);

                }

                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();
                }
                // Property Changed Event 
                public event PropertyChangedEventHandler PropertyChanged;
                private void OnPropertyChanged(string propertyname)
                {
                    var handler = PropertyChanged;
                    if (handler != null)
                        handler(this, new PropertyChangedEventArgs(propertyname));
                }
            }
        }

ModelClass i have defined my properties here

(我在这里定义了我的属性的ModelClass)

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



  ask by Ramco translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...