Here I am putting my C# WPF code including 3 files (Each for 1 model of MVVM) + View Model code file (UI.xaml.cs). I am not able to get ListBox binded with my First Text box. Need your help. What I want is as soon as I enter Text in Textbox, it should be added in listBox.
I am beginner to WPF and MVMM. Sorry if code is not much professional. Your suggestions are always welcome.
Form.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace MVVM.Models
{
public class Form : INotifyPropertyChanged
{
public Form()
{
}
#region Variables
private String _Name;
public String Name
{
get { return _Name; }
set
{
_Name = value;
OnPropertyChanged("Name");
}
}
private String _colorBack;
public String ColorBack
{
get { return _colorBack; }
set
{
_colorBack = value;
OnPropertyChanged("ColorBack");
}
}
private String _colorFont;
public String Color_Font
{
get { return _colorFont; }
set
{
_colorFont = value;
OnPropertyChanged("ColorFont");
}
}
#endregion
#region INotifyPropertyChangedMembers
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
FormViewModel.cs
using MVVM.Models;
using MVVM.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
namespace MVVM.ViewModels
{
public class FormViewModel
{
private Form _Form;
private ObservableCollection<string> listItems = new ObservableCollection<string>();
public FormViewModel()
{
_Form = new Form();
}
public Form Form
{
get { return _Form; }
}
public ObservableCollection<string> notifyChangedList()
{
listItems.Add("XYZ");
return listItems;
}
}
}
UI.xaml
<Window x:Class="MVVM.Views.UI"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:custom="clr-namespace:MVVM.ViewModels"
Title="UI" Height="300" Width="300">
<Grid>
<TextBox TextWrapping="Wrap" Text="{Binding Name}" Margin="20,15,160,225" />
<TextBox TextWrapping="Wrap" Text="Back Color" Margin="20,73,195,167" />
<TextBox TextWrapping="Wrap" Text="Font Color" Margin="20,122,195,118"/>
<ListBox Name="listBox" ItemsSource="{Binding listItems}" HorizontalAlignment="Left" Margin="156,42,0,143" Height="85" Width="93" />
<Button Content="Finish" HorizontalAlignment="Left" Margin="112,163,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
UI.xaml.cs
using MVVM.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace MVVM.Views
{
public partial class UI : Window
{
public UI()
{
InitializeComponent();
DataContext = new MVVM.ViewModels.FormViewModel();
}
}
}