开发者

DataBinding - Silverlight with XAML and WCF - fundamentals

I ha开发者_运维百科ve setup simple scenario to learn about databinding in XAML. After some thinking I concluded that's going to be essential as my project progress, so I wanted to learn some basics and understanrd it.

I have Setup Simple OperationContract with DataContract to push data from server to client:

    [DataContract]
public class TestData : INotifyPropertyChanged
{
    private string _testDataA;
    [DataMember]
    public string TestDataA
    {
        get { return _testDataA; }
        set
        {
            _testDataA = value;
            NotifyPropertyChanged("TestDataA");
        }
    }

    private string _testDataB;
    [DataMember]
    public string TestDataB
    {
        get { return _testDataB; }
        set
        {
            _testDataB = value;
            NotifyPropertyChanged("TestDataA");
        }
    }

    //public IDuplexClient RoomCallbackChannel { get; set; }
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

        [OperationContract]
    public ObservableCollection<TestData> PushTestDataToClient()
    {
        ObservableCollection<TestData> testData = new ObservableCollection<TestData>();
        testData.Add(new TestData { TestDataA = "testDataA1", TestDataB = "testDataB1" });

.. some irrevalant data adding here..

        return testData;
    }

I have added simple DataGrid to my View.

And while i do in C# code:

dataGrid1.ItemsSource = e.Result

It works perfectly. But that's not the point. I want to bind that data with XAML.

I tried to do:

dataGrid1.DataContext = e.Result

and then:

ItemsSource="{Binding TestDataA}"

and other combinations of my Model class.

It's probably silly and simple question. But I really cloudn't find, any simple example that focus on most basic thing. How to bind data from data source, with XAML ? And how it works ? How to expose that data to be available in XAML ?


This should be a full fledged tutorial to study MVVM databinding http://www.silverlight.net/learn/tutorials/silverlight-4/using-the-mvvm-pattern-in-silverlight-applications/ Hope this helps you.


I'm guessing you're missing column definitions with bindings. Here is a really good tutorial that focuses on the XAML, binding, and columns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜