Home > Blockchain >  WPF Binding to ViewModel from comboBox Selected Item using MVVM Model
WPF Binding to ViewModel from comboBox Selected Item using MVVM Model

Time:11-02

I am clearly new to WPF and MVVM, but I'm trying to understand and learn as I go. I can't seem to get the binding correct. I'm working in just a single component of the app, and I'm doing something wrong. My models are mainly coming from EF, with some minor exceptions. When I update the Combobox in this example, nothing changings, I've tried a bunch of different mechanics, and I'm just too new to get a grasp. (it was much easier in Windows Forms)

I have a UserControl

<UserControl x:Class="ClinPath.StudyManagement.Components.SM_StudyDetails"
         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:local="clr-namespace:ClinPath.StudyManagement.Components"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800"
         xmlns:uc ="clr-namespace:ClinPath.WPFControls" 
         xmlns:vm="clr-namespace:ClinPath.StudyManagement.ViewModel">


<UserControl.DataContext>
    <vm:SM_StudyDetail_VM/>
</UserControl.DataContext>


<!--#region StudyDetails-->
<Grid Grid.Row="1" Grid.ColumnSpan="9" DataContext="{Binding StudyCPV}">
    <!--#region StudyDetails Column 1-->

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>

    </Grid.RowDefinitions>
    <TextBlock Grid.Row="0" Grid.Column="0" Text="StudyName: " FontSize="12" TextAlignment="Right" />
    <TextBlock Grid.Row="1" Grid.Column="0" Text="Species: "  FontSize="12" TextAlignment="Right"/>
    <TextBlock Grid.Row="2" Grid.Column="0" Text="Study Director: " FontSize="12" TextAlignment="Right"/>
    <TextBlock Grid.Row="3" Grid.Column="0" Text="CP Coordinator: " FontSize="12" TextAlignment="Right"/>
    <TextBlock Grid.Row="4" Grid.Column="0" Text="Start Date: " FontSize="12" TextAlignment="Right"/>
    <TextBlock Grid.Row="5" Grid.Column="0" Text="Comments: " FontSize="12" TextAlignment="Right"/>

    <Grid Grid.Column="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="4*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <ComboBox IsEditable="True" Visibility="Hidden">
            <ComboBoxItem>Orange</ComboBoxItem>
            <ComboBoxItem>Apple</ComboBoxItem>
            <ComboBoxItem>Banana</ComboBoxItem>
            <ComboBoxItem>Cherry</ComboBoxItem>
            <ComboBox.Triggers>
                <EventTrigger RoutedEvent="TextBoxBase.TextChanged">
                    <BeginStoryboard>
                        <Storyboard>
                            <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsDropDownOpen">
                                <DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:0"/>
                            </BooleanAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </ComboBox.Triggers>
        </ComboBox>  

        
        <Button Grid.Column="1" Command="{Binding VerifyStudy}"/>  ICommand verify and update the source of the fields.
    </Grid>

    <ComboBox  
        x:Name ="cboStudyDirector" 
        Grid.Row="2" Grid.Column="1"  
        FontSize="12" IsEditable="True"
        ItemsSource="{Binding cboStudyDirectorV}"
        SelectedValuePath="empID" DisplayMemberPath="employeeName"
        />  //Selected Value needs TO BIND TO Field studyDirectorID in ObservableCollection cpStudy and updated the selected DropDown

    <DatePicker  x:Name ="dateStart" Grid.Row="4" Grid.Column="1"  FontSize="12"/> //NEEDS TO BIND TO Field startDate in ObservableCollection cpStudyV
    <TextBox  x:Name ="txComment" Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="3" Text="Stuff" FontSize="12"/> //NEEDS TO BIND TO Field Comment in ObservableCollection cpStudy

    <!--#endregion-->




</Grid>
<!--#endregion-->

I've also got my VM (which I know shouldn't be loading data, but I'm not sure how to do that from EF6 in .net framework 4.8)

    using ClinPath.DataSets;
using ClinPath.StudyManagement.Commands;
using ClinPath.StudyManagement.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Input;
using System.Xml.Linq;
using System.Security.Permissions;

namespace ClinPath.StudyManagement.ViewModel
{
    public partial class SM_StudyDetail_VM : ViewModelBase
    {

        private StudyManagerDB db = new StudyManagerDB();


        private ObservableCollection<StudyCP> studyCPV;
        public ObservableCollection<StudyCP> StudyCPV
        {
            get { return studyCPV; }
            set { studyCPV = value; OnPropertyChanged(nameof(StudyCPV)); }
        }

        private ObservableCollection<Species> speciesV;
        public ObservableCollection<Species> SpeciesV
        {
            get { return speciesV; }
            set { speciesV = value; OnPropertyChanged(nameof(SpeciesV)); }
        }

        public List<empComboObj> cboStudyDirectorV { get; set; }
        public List<empComboObj> cboCPCoordinatorV { get; set; }
        public List<empComboObj> cboClinicalPathologistV { get; set; }
        public List<empComboObj> cboSMCoordinatorV { get; set; }
        public List<empComboObj> cboStudySupervisorV { get; set; }
        public List<string> studyNameList { get; set; }

        public ICommand VerifyStudy { get; }

        public string studyID { get; set; }

        public ICollectionView ComboItems { get; set; }

        public string comboText;
        public string ComboText
        {
            get
            {
                return comboText;
                //throw new NotImplementedException();
            }
            set
            {
                ComboItems.Filter = item => item.ToString().ToLower().Contains(value.ToLower());
            }
        }

        public SM_StudyDetail_VM()
        {
            VerifyStudy = new SM_VerifyStudy(this);

            ObservableCollection<StudyCP> _studyCPV = new ObservableCollection<StudyCP>();
            var studyCPVList = db.StudyCPs.ToList();
            foreach (var group in studyCPVList)
            {
                _studyCPV.Add(group);


            }
            StudyCPV = _studyCPV;

            studyNameList = db.StudyCPs.Select(x => x.StudyNumber.ToString()).ToList();
            SpeciesV = new ObservableCollection<Species>(db.Species);

            cboStudyDirectorV = db.UserLists.Where(x => x.isStudyDirector == true ? true : false).Select(x => new empComboObj { empID = x.ID, employeeName = x.FirstName   " "   x.LastName }).ToList();
            cboStudySupervisorV = db.UserLists.Where(x => x.isStudySupervisor == true ? true : false).Select(x => new empComboObj { empID = x.ID, employeeName = x.FirstName   " "   x.LastName }).ToList();
            cboClinicalPathologistV = db.UserLists.Where(x => x.isClinicalPathologist == true ? true : false).Select(x => new empComboObj { empID = x.ID, employeeName = x.FirstName   " "   x.LastName }).ToList();
            cboCPCoordinatorV = db.UserLists.Where(x => x.isCPCoordinator == true ? true : false).Select(x => new empComboObj { empID = x.ID, employeeName = x.FirstName   " "   x.LastName }).ToList();
            cboSMCoordinatorV = db.UserLists.Where(x => x.isSMCoordinator == true ? true : false).Select(x => new empComboObj { empID = x.ID, employeeName = x.FirstName   " "   x.LastName }).ToList();

        }
    }
}

Lastly I have a Command

    using ClinPath.DataSets;
using ClinPath.StudyManagement.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace ClinPath.StudyManagement.Commands
{
    
    public class SM_Commands : CommandBase
    {
        public override void Execute(object parameter)
        {
            throw new NotImplementedException();
        }
    }


    public class SM_VerifyStudy : CommandBase
    {
        private StudyManagerDB db = new StudyManagerDB();

        private readonly SM_StudyDetail_VM _sM_StudyDetail_VM;

        private readonly string _studyID;
        
        public SM_VerifyStudy(SM_StudyDetail_VM sM_StudyDetail_VM)
        {
            _sM_StudyDetail_VM = sM_StudyDetail_VM;
            _studyID = sM_StudyDetail_VM.comboText;
        }

        public override void Execute(object parameter)
        {
            string studyID = _sM_StudyDetail_VM.studyID;
            _sM_StudyDetail_VM.StudyCPV = new ObservableCollection<StudyCP>(db.StudyCPs.Where(x => x.StudyNumber == studyID));
            
        }
    }
}

CodePudding user response:

Scrapped trying use WPF MVVM with this project. Moved exclusively to code behind. Working 100% now.

  • Related