Mvvm: How to bind data to a list

Source: Internet
Author: User

Use mvvm to bind a data source to a gridview

Is the telerik Control

1. defined entity docregister

Observablecollection <docregister> _ datasouce;

2. Define Object Attributes
Public observablecollection <docregister> datasouce
{
Get {return _ datasouce ;}
Set
{
If (! Referenceequals (_ datasouce, value ))
{
_ Datasouce = value;
This. raisepropertychanged () => This. datasouce );
}
}
}
3. Remember that the current class inherits from icationicationobject

4. All namespaces:
Using system;
Using system. net;
Using system. windows;
Using system. Windows. controls;
Using system. Windows. documents;
Using system. Windows. Ink;
Using system. Windows. input;
Using system. Windows. Media;
Using system. Windows. Media. animation;
Using system. Windows. shapes;
Using Microsoft. Practices. Prism. viewmodel;
Using system. Collections. objectmodel;
Using Microsoft. Practices. Prism. commands;

5. When constructing a function, use WCF to transmit data from the server.

Public docregisternavigateviewmodel ()
{
Client = innerhelper. createdocregistermanageserviceclient ();
Client. getdocregisterlistcompleted + = new eventhandler <getdocregisterlistcompletedeventargs> (client_getdocregisterlistcompleted );
// When can I call WCF?
Client. getdocregisterlistasync ("","");
}

Void client_getdocregisterlistcompleted (Object sender, getdocregisterlistcompletedeventargs E)
{
If (E. Error = NULL)
{
// Return results
Datasouce = E. result;
}
Else
{
Glodon. component. Controls. dialoghelper. alertdialog ("failed to get data! "," Prompt ");
}
}
6. Click View registered events on the page.
Private icommand _ lookcommand;
Public icommand lookcommand
{
Get {return _ lookcommand ?? (_ Lookcommand = new delegatecommand <Object> (this. onlookcommand ));}
}

7. When you click "View" to view the connection of each line, pass the value to the past (create a new page and pass the selected row value to the past)
Private void onlookcommand (Object OBJ)
{
If (currentitem! = NULL)
{
Adddocregister editwindow = new adddocregister (currentitem );
Editwindow. Show ();
}
}
8. XAML file: Pay attention to the use of command

<TR: radgridview horizontalalignment = "Left" name = "radgridview" verticalignment = "TOP" itemssource = "{binding datasouce, mode = twoway}" selecteditem = "{binding currentitem, mode = twoway }"
Autogeneratecolumns = "false" showgrouppanel = "false" grid. Row = "1">
<TR: radgridview. Columns>

<TR: gridviewcolumn header = "View" width = "60" headertextalignment = "center">
<TR: gridviewcolumn. celltemplate>
<Datatemplate>
<Hyperlinkbutton name = "viewbutton" tag = "{binding docregisteradministrativeid}" content = "View" command = "{binding source = {staticresource datacontextproxy}, Path = datasource. lookcommand} "commandparameter =" {binding elementname = viewbutton} "/>
</Datatemplate>
</TR: gridviewcolumn. celltemplate>
</TR: gridviewcolumn>

<TR: gridviewdatacolumn isreadonly = "true" header = "no." isfilterable = "false" width = "60" textalignment = "center" headertextalignment = "center"/>
<TR: gridviewdatacolumn isreadonly = "true" datamemberbinding = "{binding receiptdate, mode = oneway} "header =" recipient date "isfilterable =" false "width =" 150 "headertextalignment =" center "/>
<TR: gridviewdatacolumn isreadonly = "true" datamemberbinding = "{binding dnumber, mode = oneway} "header =" "isfilterable =" false "width =" 120 "headertextalignment =" center "/>
<TR: gridviewdatacolumn isreadonly = "true" datamemberbinding = "{binding dFrom, mode = oneway} "header =" Communications Authority "isfilterable =" false "width =" * "headertextalignment =" center "/>
<TR: gridviewdatacolumn isreadonly = "true" datamemberbinding = "{binding dtitle, mode = oneway} "header =" file title "isfilterable =" false "width =" * "headertextalignment =" center "/>
<TR: gridviewdatacolumn isreadonly = "true" datamemberbinding = "{binding dconfidentialityid, mode = oneway} "header =" confidential level "isfilterable =" false "width =" 60 "headertextalignment =" center "/>
<TR: gridviewdatacolumn isreadonly = "true" datamemberbinding = "{binding filecount, mode = oneway} "header =" number of parts in the body "isfilterable =" false "width =" 60 "headertextalignment =" center "/>
<TR: gridviewdatacolumn isreadonly = "true" datamemberbinding = "{binding attachmentcount, mode = oneway} "header =" Number of attachments "isfilterable =" false "width =" 60 "headertextalignment =" center "/>
<TR: gridviewdatacolumn isreadonly = "true" datamemberbinding = "{binding dstate, mode = oneway} "header =" status "isfilterable =" false "width =" 80 "headertextalignment =" center "/>
</TR: radgridview. Columns>
</TR: radgridview>

(1) datacontextproxy has such a line of code at the beginning of the page. CS file, which can be used under the current XAML File
<Usercontrol. Resources>
<Helper: datacontextproxy X: Key = "datacontextproxy"/>
</Usercontrol. Resources>

Datacontextproxy Definition

using System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.Windows.Data;namespace Glodon.Module.DocRegisterManage.Help{    public class DataContextProxy : FrameworkElement    {        public DataContextProxy()        {            this.Loaded += new RoutedEventHandler(DataContextProxy_Loaded);        }        void DataContextProxy_Loaded(object sender, RoutedEventArgs e)        {            Binding binding = new Binding();            if (!String.IsNullOrEmpty(BindingPropertyName))            {                binding.Path = new PropertyPath(BindingPropertyName);            }            binding.Source = this.DataContext;            binding.Mode = BindingMode;            this.SetBinding(DataContextProxy.DataSourceProperty, binding);        }        public Object DataSource        {            get { return (Object)GetValue(DataSourceProperty); }            set { SetValue(DataSourceProperty, value); }        }        public static readonly DependencyProperty DataSourceProperty =            DependencyProperty.Register("DataSource", typeof(Object), typeof(DataContextProxy), null);        public string BindingPropertyName { get; set; }        public BindingMode BindingMode { get; set; }    }}

9. The main step is to bind the viewmodel with the page
During the initialization of the XAML page, specify a viewmodel: docregisternavigateviewmodel for the current context datacontext Of The XAML.

Public partial class docregisternavigateview: usercontrol
{
Public docregisternavigateview ()
{
Initializecomponent ();
This. datacontext = new docregisternavigateviewmodel ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.