Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Binding Textbox in MVVM Light

$
0
0

Through the oldWinForm I realizeda small andsimple application that usesthe Entity Framework6.

There are 3Textbox: ID, Name, Surnameand a Button.

Model:

[Table("Customer")]
    public partial class Customer
    {
        public int ID { get; set; }

        [StringLength(50)]
        public string Name { get; set; }

        [StringLength(50)]
        public string Surname { get; set; }
    }

This is the code:

namespace WindowsForms
{
    public partial class MainForm : Form
    {
        private MyContext _ctx;
        public MainForm()
        {
            InitializeComponent();
        }
        private void onLoad(object sender, EventArgs e)
        {
            _ctx = new MyContext();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            customerBindingSource.DataSource = _ctx.Customers.Find(1);
        }
    }
}

My goalis to transformthe codein WPFwithMVVMLight.

Then I openeda new project andinstalled theMVVM Lightpattern:

<Window x:Class="MvvmLightForms.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:ignore="http://www.ignore.com"
        mc:Ignorable="d ignore"
        Height="300"
        Width="300"
        Title="MVVM Light Application"
        DataContext="{Binding Main, Source={StaticResource Locator}}"><Window.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="Skins/MainSkin.xaml" /></ResourceDictionary.MergedDictionaries></ResourceDictionary></Window.Resources><Grid x:Name="LayoutRoot"><TextBlock FontSize="36"
                   FontWeight="Bold"
                   Foreground="Purple"
                   Text="{Binding WelcomeTitle}"
                   VerticalAlignment="Center"
                   HorizontalAlignment="Center"
                   TextWrapping="Wrap" /><Button Content="Button" Command="{Binding LoadCommand}" HorizontalAlignment="Left" Margin="26,22,0,0" VerticalAlignment="Top" Width="75"/><TextBox HorizontalAlignment="Left" Height="23" Margin="26,68,0,0" TextWrapping="Wrap" Text="{Binding Path=Id}" VerticalAlignment="Top" Width="120"/><TextBox HorizontalAlignment="Left" Height="23" Margin="26,96,0,0" TextWrapping="Wrap" Text="{Binding Path=Name}" VerticalAlignment="Top" Width="120"/><TextBox HorizontalAlignment="Left" Height="23" Margin="26,124,0,0" TextWrapping="Wrap" Text="{Binding Path=Surname}" VerticalAlignment="Top" Width="120"/></Grid></Window>

MainViewModel:

namespace MvvmLightForms.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        private Customer _customer;
        private MyContext _ctx;

        private RelayCommand loadCommand;

        public ICommand LoadCommand
        {
            get
            {
                if (loadCommand == null)
                    loadCommand = new RelayCommand(() => Load());
                return loadCommand;
            }
        }

        private void Load()
        {
            _customer = _ctx.Customers.Find(1);
        }
        public MainViewModel()
        {
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
            }
            else
            {
                // Code runs "for real"
                _ctx = new MyContext();
            }
        }
    }
}
Howeverit worksonly inpart.In the sensethatthe code willcompileproperly (withouterrors),butdoes not behavelikeWinforms.What should Iadd?(or correct?)




Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>