Pages

Showing posts with label Binding. Show all posts
Showing posts with label Binding. Show all posts

Wednesday, November 10, 2010

Binding Silverlight DataGrid with XML file

Hello All,
In this article I would like to discuss about binding value to a Silver light DataGrid. As we know that there is no direct support for the Dataset or DataTable in the Silver light, so how can we bind the grid??? See there are multiple option for binding the grid in silver light either we can use the-
  1. 1. Collection of the Item Class,
  2. or a Dictionary or
  3. we can also use a XML File
In this article I am going to describe the way to bind a XML file that is present in the server Side, that means a User XML file can be bind to the grid in the Silver light form.Here I am Making use of LINQ to read the data from XMl file.


first We Will see that how can we access the XML file and its element. Please check the below Code.

private void getDatFromXML()
{
Uri newURL = new Uri("mydata.xml", UriKind.Relative);
WebClient cnt = new WebClient();
cnt.DownloadStringCompleted += new DownloadStringCompletedEventHandler(cnt_DownloadStringCompleted);
cnt.DownloadStringAsync(url);
}

Private void cnt_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
dataGrid1.ItemsSource = ReadListItem(e.Result);
}

public List<Item> ReadListItem(string xmlContent)
{
List<Item> il = new List<Item>();
XElement doc = XElement.Parse(xmlContent);
il = (from ele in doc.Elements()
select GetItem(ele)).ToList();
return il;
}

You have seen that how first we downloaded the file from the server machine and then reading its content to bind the Grid. Here ReadListItem is a generic function which returns us the LIST ITEM collection, the collection can be easily bind to the Silver light data grid. We just have to set the ItemSource property of the Silver Light Data Grid. like.

dataGrid1.ItemsSource = ReadListItem(e.Result);


I hope you have Got the Idea. Please look at the XAML part of the XML file that how the content looks.


<UserControl x:Class="DataBinding.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
d:DesignHeight="513" d:DesignWidth="662" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

<Grid x:Name="LayoutRoot" Background="White" Height="600" Width="600">
<sdk:DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="20,30,0,0" Name="dataGrid1" VerticalAlignment="Top"Width="450" />
</Grid>
</UserControl>


Hope You Understood the Way to bind the DataGrid in Silver light by a XML file. Please feel free to post the comments.

Thanks,
Anil Kumar Pandey
MVP (Visual C#)

Wednesday, September 8, 2010

Data Binding Directions in WPF

Hello All,
As you all know that binding in WPF is simple with the help of "Binding" keyword we can bind the Item to any data source. For binding there must be 2 thing "Source" & "Target", The target can be any any Control or Property derived from DependencyProperty and the Source can be any of the following

  • Public Property of a control
  • CLR(Common Language Runtime) Object
  • XML Element
  • A Dataset
  • and the Static Resource
Text="{Binding ElementName=lbSource}"

Here I am going to discuss the various data binding direction available in the WPF. There are 4 different kind of binding Direction are available we can use them according to our need the various binding directions are.

  1. OneWay
  2. OneWayToSource
  3. OneTime
  4. TwoWay
OneWay :- The most common and the first direction used in the WPF data binding is OneWay. this is the default binding direction which does not need to be specified, in this direction the data always flow from Source to target. The TEXT property of the textblock default binding is OneWay. This is useful when we just want to display the result to the users.


OneWayToSource :- This is also similar to the OneWay binding but the main difference between them is , when we use the OneWayToSource direction the data flows from Target to Source. This can be useful when we want that a user can change the value in the back end.

OneTime :- OneTime property is also similar to the OneWay binding direction, but the thing is that using this option the change in the source not always reflect to the target, the target get updated only when the application load or there is a context change. This is pretty useful when we want to display a user a ReadOnly type of value which does not get change each time.

TwoWay :- This is the useful binding direction in the WPF, when we want that the value can be changed from either side. Here the changes get reflected each time a source value is changed or the target value is changed. This can be quite useful when we want to display the value to the user as well as allow user to change the the value in the back end.

Hope you all have understood waht all are the data binding direction in the WPF, as I always says thing are not complex until we know them. Here are examples for using the binding.

<StackPanel>
<TextBlock Width="248" Height="24" Text="Colors:"
TextWrapping="Wrap"/>
<ListBox x:Name="lbSource" Width="248" Height="56">
<ListBoxItem Content="Blue"/>
<ListBoxItem Content="Green"/>
<ListBoxItem Content="Yellow"/>
</ListBox>

<TextBlock Width="100" Height="30" Text="You selected color:" />

<TextBlock Width="100" Height="30">
<TextBlock.Text>
<Binding ElementName="lbSource" Path="SelectedItem.Content"/>
</TextBlock.Text>
</TextBlock>
</StackPanel>



I hope this post will help in understanding the things of bindings in WPF. Please feel free to post the comments.

Thanks
Anil Kumar Pandey
MVP (Visual c#)

Kontera