Follow me WPF:M-V-VM model development

See this forum to explore WPF friends understood, and this is a very good Windows App development technology, so in the next to open a paste, initiate, hope you can inspire enthusiasm. Too late now, if you are interested in the first reply reply, if you see a friend want to learn, I will explain in detail in this post.

Started by Chris at February 04, 2016 - 6:36 AM

Look forward to. . . . . .

Posted by Brian at February 12, 2016 - 6:48 AM

Waiting for you to explain. . .....

Posted by Brian at February 20, 2016 - 7:37 AM

Ha-ha. . .
LZ the dedication.
M-V-VM
The wrong plate. .

You go to Silverlight board. .

Posted by Bowen at March 07, 2016 - 8:18 AM

The reply to 2011-03-12 09:02:11 was a moderator to delete

Posted by Hubery at March 10, 2016 - 8:55 AM

Well, these 2 days to see some relevant knowledge, and MVC, MVP, difference.

Don't want to LZ the word only from MVVM, if you can expect from WPF why use MVVM instead of other, what properties using the MVVM dependent on WPF, whether SL can be fully realized.

In addition to the model of event WINFORM, discuss the 3 or more layers of many models, their implementation and MVVM directly and the advantages and disadvantages of the. Of course, there are the best examples. I think this is not understand MVC, MVVM people most want to know

Posted by Mayme at March 14, 2016 - 9:54 AM

From the WinForm 15 years ago, to the nearest 10 years (such as asp.net from version 1 has been), Microsoft has been on one-way/two-way Binding technology. Just a beginner with "we are beginners, I the biggest" for reasons not to understand.

Posted by Stan at March 15, 2016 - 10:04 AM

The reply to 2011-03-12 09:05:58 was a moderator to delete

Posted by Magee at March 17, 2016 - 10:15 AM

There are people reply! Well, I'll throw bricks. Below to begin. The first reply about wxr0323: I think this page you are asking WinForm, hope that everyone concerned about the WPF, so we send in this area. Following a formal start...... To throw code will let many people feel bored, so I'll make some text introduction -- of course -- I won't Google just copy and paste something over, I mainly talk about my understanding. If some people think do, can be "critical" and correct in the following.

When I wrote this thing, many people have started to try WPF. But the problem is, many of them are still in Designer direct drag a control, and automatic generation of XAML, then double-click the different controls to add code behind (also known as code). In this way, you'll find out there's no difference between WPF and WinForm, WPF is a XAML version of the WinForm only, but it's Win7UI's skin. But that is not the case, although in theory, WPF can do things WinForm can do, but they are different in the realization of the thoughts and methods.

WinForm, Irresponsible speaking (sorry), is on the event driven between to complete the UI and UI, the interaction between the UI and the data -- -- this creates a potential problem: this model is a bit like the process oriented programming, the problem is the maintenance and reusability of code. Of course, this is not to attack WinForm, until today, it still is in many ways has its own use.

Let's look at WPF. Why MVVM mode will be many developers as the preferred (in fact basically is required)? This must start from the understanding of the four letters. MVVM is actually M-V-VM:M Model, V View, VM ViewModel. Model it is a pile of a data type, View it is UI, or XAML. Focuses on ViewModel, and this is where the MVVM core. ViewModel linking UI and data, make it can communicate freely. This will not only reduce the coupling of data with UI, and the UI abstract into an entity class, can maximize the realization of object oriented programming -- -- object oriented benefits I think I Needless to say.

Such a stack of said, I was not really related to MVVM, because, first of all beginners to WPF data is bound to have a rough understanding of. As sp1234 said, long before Data Binding has existed, but how many people really seriously investigated.?

A the beginning of everything: DataContext
This is the first chapter of this tutorial. A very simple case we. First, add a new WPF Project, named DataContextSample. Then add a class Student, as follows:
namespace DataContextSample
{
    internal class Student
    {
        #region Properties
        public string Name
        {
            get;
            set;
        }
        #endregion // Properties
    }
}


Open your MainWindow.xaml, and then copy and paste the code I:
<Window x:Class="DataContextSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
            <Label>Name:</Label>
            <TextBox x:Name="NameTextBox" Height="23" Width="150" />
            <Button x:Name="ShowNameButton" Margin="2,0">Show Me Name</Button>
        </StackPanel>
    </Grid>
</Window>


Our goal is: after running the program, pass a Student object to the MainWindow, and then allow the user to change the object's Name in TextBox, then click the button, will change after the Name MessageBox returns to the user. We first look at the WinForm mode is used to achieve this point.

Open your MainWindow.xaml.cs, copy and paste the code I:
using System.Windows;

namespace DataContextSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Fields
        private readonly Student _student;
        #endregion // Fields

        #region Constructors
        public MainWindow()
        {
            _student = new Student() { Name = "New Student" };
            InitializeComponent();
            this.ShowNameButton.Click += this.ShowNameButton_Click;
            this.Loaded += this.OnLoaded;
        }
        #endregion // Constructors

        #region Event Handlers
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            NameTextBox.Text = _student.Name;
        }

        private void ShowNameButton_Click(object sender, RoutedEventArgs e)
        {
            _student.Name = NameTextBox.Text;
            MessageBox.Show(_student.Name);
        }
        #endregion // Event Handlers
    }
}


Run the program, try to enter a different name in TextBox, then click on the "Show Me Name", have a look whether we achieve the expected goal. This procedure may seem silly, some people will ask: why not direct output NameTextBox stuff? Never mind, this is a small example, I hope that through the Student class to show WPF UI is how to interact with the data. Believe that the WinForm approach all understand, nothing but is loaded in MainWindow after the Student Name is assigned to NameTextBox, and then click the button event, NameTextBox Text to Student, and then returned to the user.

However, there is another solution to this problem. The following code into the open MainWindow.xaml,:
<Window x:Class="DataContextSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
            <Label>Name:</Label>
            <TextBox Text="{Binding Path=Name, Mode=TwoWay}" Height="23" Width="150" />
            <Button x:Name="ShowNameButton" Margin="2,0">Show Me Name</Button>
        </StackPanel>
    </Grid>
</Window>


Then the MainWindow.xaml.cs code was renamed:
using System.Windows;

namespace DataContextSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Fields
        private readonly Student _student;
        #endregion // Fields

        #region Constructors
        public MainWindow()
        {
            _student = new Student() { Name = "New Student" };
            InitializeComponent();
            this.DataContext = _student;
            this.ShowNameButton.Click += this.ShowNameButton_Click;
        }
        #endregion // Constructors

        #region Event Handlers
        private void ShowNameButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(_student.Name);
        }
        #endregion // Event Handlers
    }
}


Run the application again, you'll find out we have achieved expected effect. If the comparison of two methods, you may say: after a it was before a write a few OnLoaded, and saves the _student.Name = NameTextBox.Text. Don't worry, I said, this is just a small example, often small things can reflect the changes in. Although we in the amount of code on the changes very little, but the idea behind it is different. I run so much, come back later to explain.

Posted by Chris at March 30, 2016 - 10:54 AM

<TextBox Text="{Binding Path=Name, Mode=TwoWay}" Height="23" Width="150" />
_student = new Student() { Name = "New Student" };
See here, I don't want to look down.
Bad design,
Or rather, it simply does not design, more don't talk about object oriented design.

Posted by Jessie at April 03, 2016 - 11:45 AM

You may not see my text, this example simply talk about the concept of DataContext, there does not exist any design...... I didn't come here to show my design. For starters, the first thing to do is to understand the concept of. But for such a simple example of the demand, why go with what design, to illustrate the problem can be.

Posted by Chris at April 05, 2016 - 12:07 PM

The reply to 2011-03-12 10:00:01 was a moderator to delete

Posted by Earl at April 13, 2016 - 12:23 PM

If you want to borrow your reply to me, to show you the wise, the way you or not: 1 if you have better and more simple example to illustrate the DataContext, you can stick to let everyone learn to explore. 2 here is not the code. Not to show the code can also be, I can take a variety of design ten years out (of which 5 years in Microsoft), you want to look bad.?

Posted by Chris at April 27, 2016 - 1:23 PM

You have heart, can tidy written blog..

Posted by David at April 28, 2016 - 1:59 PM

I say: I write these things without any return. I said, if I do, you can criticize and correct, but the premise is you have to read what I write, and show your reason. Microtry like this without any reason "criticize", I will spare no effort to counter. The red star may not green shorts to teach others? Ha ha, you then hate.

Posted by Chris at April 30, 2016 - 2:28 PM

The reply to 2011-03-14 08:55:08 was a moderator to delete

Posted by Nathan at May 11, 2016 - 2:30 PM

Although I have no contact with WPF, but I still support this post will continue, as some have no WPF experience and ready to learn WPF do a certain foundation, strong support for the continued, desire for knowledge, good is not it?

Posted by Booth at May 19, 2016 - 2:41 PM

Thank you for your support. I will continue to.

Posted by Chris at May 23, 2016 - 3:40 PM

After all, WPF is known as the windows application programming framework of next generation. Support LZ to continue.
But LZ is to start from scratch, the difficulty is too big.
The most important application of DP binding, is enough to open a few posts.

Posted by Nat at June 07, 2016 - 3:52 PM

Thank you for your support. Did not want to speak, is the most basic MVVM modes of thinking about, and then let the beginners to find something more. Dependency Property WPF is their own things, don't put it in the MVVM. If MVVM can be finished, and I say these other WPF related.

Posted by Chris at June 17, 2016 - 4:03 PM

From the title, so I understand:
1 you are a teacher (if you say you are a student, I do really harsh requirements);
2 with you to learn MVVM design pattern (this is from the layered design is similar to the MVC/MVP derived model)
(separation of duties and the introduction of virtual assembly and improve relations between other components is the core idea of this kind of design pattern);
The 3 most typical I picked out 2 lines of code design problems,
(I think you will use the actual project code teaching question, so, you mean you don't actually write code);
4 don't say in Microsoft 5 years, some people a lifetime under the chess are not.;
5 if I which one is not telling the truth, or against the landlord I comment, please correct me, take out your idea to me, I'm not embarrassed, but will be very happy, I was rookie, but I'm not blind bull's point of view, put forward different views I absolutely attitude bright;
6 if making personal attacks, I will not.

Posted by Jessie at June 29, 2016 - 4:55 PM

But do not know this in small projects in? For this is not understood is going to study at MVC SL in

Posted by Vicky at July 02, 2016 - 5:45 PM

I will not use the actual project code to teaching, for beginners do not understand. If a person even DataContext didn't understand, the so-called project code for teaching can play what help? I don't know your level, but I am not a rookie, so I'm out throwing bricks. I'm not on you deny me, my opinion of you is: you didn't read what I wrote, and then pick out the two line of code is what design is not design problem -- it is groundless statement. Because it is a very simple example, for beginners to understand the DataContext attribute can be used XAML language to the two-way binding. You are for practical project, and I is for beginners. Not any beginners was eager to begin to see tens of thousands of so-called project looks rather heroic code, that none of them a little help. I repeat: I write code does not contain any useful design idea, just to give beginners a very simple example, after all the text is too boring.

Some people a lifetime game isn't -- I what is the background of what level do not need you to question. This post, some people find it useful, I hope I continue, I will continue to. You are not satisfied to leave, because I don't have anything to do with you. You might not understand that now: teaching is not equal to the actual projects show the code, especially in the early stage. In the above example, I will improve one point one point spread, the purpose is to let beginners gradual understanding of the whole process. Do you like challenge me, you can throw your project code, you look for beginners can accept? It is not those things, my ten years than you see may be more than a little more or less, you in there with me ".

Posted by Chris at July 03, 2016 - 5:59 PM

You say which one is true, you win. You must go to a Hello World program design, then I have nothing to say. Why don't you say no interface, generic, why don't you say no to set the ValidationRule to TextBox, how do you say I did not handle the exception. -- in case the user input to faint, this program should be immediately handle this exception and give users to call an ambulance. I then paste the project code.

Posted by Chris at July 04, 2016 - 6:49 PM

MS digress. Who don't also entangled. Continue.

Posted by Nat at July 17, 2016 - 7:05 PM

Oh, I didn't say: follow me hello world:XXX model development

Posted by Jessie at November 23, 2016 - 3:00 AM

Oh, master, I'm wrong. I really should not write to the Hello World. If I have the chance to teach you, I will use Goodbye World.

Posted by Chris at December 08, 2016 - 3:08 AM

If there is misleading, I think we can correct me criticism. But I don't know why, "stop" share?

Posted by Ed at December 11, 2016 - 3:48 AM

Because I slobber war already. Do not fight, I surrender, but some people still want to see, so I continue to do.

Posted by Chris at December 20, 2016 - 3:53 AM

The reply to 2011-03-14 15:22:58 was a moderator to delete

Posted by Curitis at December 21, 2016 - 4:21 AM

The lecture is always from the shallower to the deeper, the landlord on the very good, be good at giving systematic guidance of traditional Winform into WPF. Then let people understand what is the advantage of WPF over Winform, and then said, very good. Some people have mental illness, like drilling horns. The landlord can continue to.

Posted by Elmer at December 23, 2016 - 5:00 AM

Ask how long it takes you to get out of a chapter, read the content feeling okay, can explain the problem on the line, no need to make how profound, everyone from Hello World to start, never mind!.

Posted by Curitis at December 26, 2016 - 5:33 AM

I usually have a time to supplement the weekend. Now then, tell. We mainly concern second solutions to above problems. This solution is: we are not in the ShowNameButton_Click method, the NameTextBox value is passed to the Student object, then the Student Name is how to change? In the OnLoaded method, is a very key: this.DataContext = _student; here, we finally saw DataContext, then come and chat to talk about it.

Context itself is the meaning of the environment, background etc. Then the DataContext direct translation is the data context. In WPF, each component has a property of type DataContext object. Why is object? Because object is all types of root, that is to say the object DataContext supports all types of. Once the MainWindow has its own DataContext, which is Student, it became clear in the data in the XAML state.
<TextBox Text="{Binding Path=Name, Mode=TwoWay}" Height="23" Width="150" />

This code is also very important, it plays a role of informing the TextBox: you are bound to the Name property of the current DataContext. If the Name attribute is bound, and Mode is a two-way, and be bound class (Student) of the Name attribute can be external changes, then NameTextBox will automatically convert Text value to the Student Name, which do not require any code is very simple --. And this binding can identify different data types. For example: the Age attribute to the Student you can add a Int, and then bind it to another TextBox, you'll find out even if you input the data type for the string "32", "18" type, the Age property will still be safe assignment, because WPF does it work as a transition between you (about conversion problems later, don't worry).

You should now have a general understanding of the -- is mainly convenient data binding to DataContext. However, we still have many issues not resolved. For example: since NameTextBox has been bound, then how is it know when to pass values to the Student? When each time its TextChanged, or when, in his LostFocus and so on, and whether there is only two-way binding...... All of these problems, are all about the binding problem, we leave to the next chapter.

DataContext introductory temporarily preaching here, leave a small operation: Student add some attribute, then try to use different control to bind. For example: Student plus a gender attributes (data types you can customize), add two RadioButton, each Male and Female, and then see if you can put the sex attribute success to bind to the two RadioButton (more than one way, the next time we will have a look when bound to how many methods, and find out the better method).

If you want to learn WPF, carefully read what I wrote, it would help, you persist in learning, I also insist on writing, thank you support.

Posted by Chris at January 03, 2017 - 6:19 AM

I want to know how to get in the XP font clear...

Posted by David at January 09, 2017 - 6:34 AM

Your question is too vague.... How to answer.

Posted by Chris at January 14, 2017 - 6:26 AM