Search This Blog

Tuesday, November 19, 2013

WPF Binding to Web Service Example

WPF > Binding > Web Service

Add reference to your Web service. We will use com.microsoft.msdn.services as example:

 


XAML:


<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="300"
       xmlns:src="clr-namespace:WpfApplication2"
        >
    <Grid>
        <ListView  ItemsSource="{Binding Path=availableVersionsAndLocales}" HorizontalAlignment="Left" Height="240" VerticalAlignment="Top" Width="266">
            <ListView.View>
                <GridView>
                    <GridViewColumn  DisplayMemberBinding="{Binding Path=locale}" Header="Locale" />
                    <GridViewColumn  DisplayMemberBinding="{Binding Path=version}" Header="Version" />
                </GridView>
            </ListView.View>
        </ListView>
   </Grid>
</Window>

C# Code:

using WpfApplication2.com.microsoft.msdn.services;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    ///
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            getContentRequest request = new getContentRequest();
            request.contentIdentifier = "abhtw0f1";
            ContentService proxy = new ContentService();
            this.DataContext = proxy.GetContent(request);
        }
    }
}

Result: