|
|||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThe Using DataGrid in a Silverlight ApplicationThe xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
(Dragging from the tool box will automatically add this namespace.) Then, the <my:DataGrid Height="100" Width="100"></my:DataGrid>
Column TypesThe
When the <data:DataGrid.Columns>
<data:DataGridTextBoxColumn
Header="Address"
Width="120"
DisplayMemberBinding="{Binding Address}" >
<data:DataGridTextBoxColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" value="Wrap"/>
</Style>
</data:DataGridTextBoxColumn.ElementStyle>
<data:DataGridTextBoxColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="Foreground" Value="Blue"/>
</Style>
</data:DataGridTextBoxColumn.EditingElementStyle>
</data:DataGridTextBoxColumn>
<data:DataGridCheckBoxColumn
Header="Subscribed?"
Width="75"
DisplayMemberBinding="{Binding IsSubscribed}"
IsThreeState="True" />
</data:DataGrid.Columns>
New FunctionalitiesSilverlight introduces new properties to enhance the functionalities of the
<data:DataGrid x:Name="dataGrid3"
RowDetailsVisibility="VisibleWhenSelected" >
<data:DataGrid.RowDetailsTemplate>
<DataTemplate>
<TextBlock Text="Address: />
</DataTemplate>
</data:DataGrid.RowDetailsTemplate>
</data:DataGrid>
The following is a
SelectionMode: The selection mode options are SingleFullRow and ExtendedFullRow. When set to ExtendedFullRow, the user can select multiple rows.Opacity: The opacity of the DataGrid can be changed by using this property.For the complete list of properties, go to MSDN. BindingTo bind a <DataGridTextBoxColumn DisplayMemberBinding="{Binding DataFieldName}" >
<my:DataGrid.Columns>
<my:DataGridTextBoxColumn DisplayMemberBinding="{Binding ID}"
IsReadOnly="False"></my:DataGridTextBoxColumn>
<my:DataGridTemplateColumn>
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding FName}" FontSize="10"/>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
<my:DataGridTextBoxColumn DisplayMemberBinding="{Binding LName}"
IsReadOnly="False" ></my:DataGridTextBoxColumn>
</my:DataGrid.Columns>
Binding to a private void BindToGrid()
{
Employee[] empObj = new Employee[5];
empObj[0] = new Employee("EmpFName1", "EmpLName1", "1");
empObj[1] = new Employee("EmpFName2", "EmpLName2", "2");
empObj[2] = new Employee("EmpFName3", "EmpLName3", "3");
empObj[3] = new Employee("EmpFName4", "EmpLName4", "4");
empObj[4] = new Employee("EmpFName5", "EmpLName5", "5");
//Binding to Datagrid
dgSample.ItemsSource = empObj;
}
In the example, the public class Employee
{
public Employee(string FName, string LName, string ID)
{
this.FName = FName;this.LName = LName;this.ID = ID;
}
public string ID
{
get;set;
}
public string FName
{
get;set;
}
public string LName
{
get;set;
}
}
Applying StyleThe <DataGrid x:Name="dataGrid1"
Style="{StaticResource DataGridStyle}"
ColumnHeaderStyle="{StaticResource ColumnHeaderStyle}"
RowHeaderStyle="{StaticResource RowHeaderStyle}"
RowStyle="{StaticResource RowStyle}" >
<DataGrid.Columns>
<DataGridTextBoxColumn
DisplayMemberBinding="{Binding DataFieldName}"
CellStyle="{StaticResource CellStyle}" />
</DataGrid.Columns>
</DataGrid>
For an overview on styling in Silverlight, see: Styling and Templating Overview. Editable a DataGrid
One can use two way binding to reduce code. But, in most cases, we may need to catch the above events. private void dgSample_CommitCellEdit(object sender, DataGridCellEventArgs e)
{
string strChangedText = ((TextBlock)e.Element).Text;
//Add Logic to update the datasource
}
Sorting and PagingThe You can find one such implementation here.
|
||||||||||||||||||||||||||||||||||||||||||||