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

Practicing using MVVM, having some very minor issues

$
0
0

Essentially, I have finally understood how to use MVVM in theory and trying to make a practical application that utlizies MVVM. I was able to get about half of the program to work correctly, however, there is one little slump that is getting me. The program calls information from an XML file and puts one portion into the listbox (that works correctly), however, nothing will display in my listbox or textbox as intended. if I tweak the Xaml Code for the TestingMVVM.View, I can have the label and textbox display the word in the listbox.Maybe someone could look at my code and guide me in the direction to fix the remaining portion.

My XAML for the View:

<UserControl x:Class="TestingMVVM.View"
             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:TestingMVVM"
             xmlns:vm="ViewModel"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300"><UserControl.Resources><local:ViewModel x:Key="viewModel"/></UserControl.Resources><Grid><ListBox x:Name="listBox" HorizontalAlignment="Left" Margin="10,82,0,10" Width="100" DataContext="{Binding English}"/><Label x:Name="label" Content="{Binding Path=Romaji, UpdateSourceTrigger=PropertyChanged}" Margin="132,143,48,138"/><TextBox x:Name="textBox" Height="23" Margin="132,0,48,110" TextWrapping="Wrap" DataContext="{Binding Japanese, UpdateSourceTrigger=PropertyChanged}" Text="{Binding ElementName=listBox, Path=SelectedItem}" VerticalAlignment="Bottom"/><Button x:Name="button" Content="Button" Margin="148,0,77,37" VerticalAlignment="Bottom"/></Grid></UserControl>

C# Code behind for View

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace TestingMVVM
{
    /// <summary>
    /// Interaction logic for View.xaml
    /// </summary>
    public partial class View : UserControl
    {
        private const string _cXmlPath = @"c:\users\jesse\documents\visual studio 2015\Projects\WpfApplication1\WpfApplication1\DictionaryData.xml";
        private const string _cWordName = "Word";
        private const string _cEnglishName = "English";
        private const string _cRomajiName = "Romaji";
        private const string _cJapaneseName = "Japanese";

        public View()
        {
            InitializeComponent();
            DataContext = new ViewModel();
            XDocument xdoc = XDocument.Load(_cXmlPath);

            listBox.ItemsSource = GetWords(xdoc, _cEnglishName);
        }

        private List<string> GetWords(XDocument xdoc, string name)
        {
            var words = from wordList in xdoc.Root.Elements(_cWordName)
                        select wordList.Element(name).Value;
            return words.ToList();
        }
    }
}


My ViewModel:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestingMVVM
{
    public class ViewModel : INotifyPropertyChanged
    {
        private Model m_model;

        public ViewModel()

        {
            m_model = new Model();
        }

        public string English
        {
            get { return m_model.English; }
            set
            {
                if (m_model.English != value)
                {
                    m_model.English = value;
                    InvokePropertyChanged("English");
                }
            }
        }
        public string Romaji
        {
            get { return m_model.Romaji; }
            set
            {
                if (m_model.Romaji != value)
                {
                    m_model.Romaji = value;
                    InvokePropertyChanged("Romaji");
                }
            }
        }
        public string Japanese
        {
            get { return m_model.Japanese; }
            set
            {
                if (m_model.Japanese != value)
                {
                    m_model.Japanese = value;
                    InvokePropertyChanged("Japanese");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void InvokePropertyChanged(string propertyName)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            PropertyChangedEventHandler changed = PropertyChanged;
            if (changed != null) changed(this, e);
        }
    }
}

And my Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestingMVVM
{
    public class Model
    {
        public string English { get; set; }
        public string Romaji { get; set; }
        public string Japanese { get; set; }
    }
}





Viewing all articles
Browse latest Browse all 18858

Trending Articles



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