Search This Blog

Friday, October 28, 2011

Silverlight DataGrid dynamic binding in DataGridTemplateColumn

The problem is that we want to bind a field from a custom object. This field is of type different from string (we could use DataGridTextColumn and set its binding like this:

DataGridColumns.Add(new DataGridTextColumn());
((DataGridBoundColumn) DataGridColumns[DataGridColumns.Count - 1]).Binding = new Binding() {Path = new PropertyPath("A")};    )

The only other Column type in SL4 is DataGridTemplateColumn. So we have to create a template, insert a TextBox into it and bind its text to our property. The solution looks as follows:

 

DataGridColumns.Add(new DataGridTemplateColumn());
string templateString = "<DataTemplate x:Key=\"CustomDataGridColumnTemplate\" "
    + "xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"  "
    + "xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" >"
    + "<TextBox Name=\"CustomTextBox\" Text=\"{Binding [0]}\" />"
    + "</DataTemplate>";
templateString = templateString.Replace("[0]", "A");
DataTemplate mycolumntemplate = XamlReader.Load(templateString) as DataTemplate;
((DataGridTemplateColumn) DataGridColumns[DataGridColumns.Count - 1]).CellTemplate = mycolumntemplate;
DataGridColumns[DataGridColumns.Count - 1].Header = browserColumn.Name;
DataGridColumns[DataGridColumns.Count - 1].Width = DataGridLength.Auto;

 

Note: You don’t need to include any namespaces if you don’t use x:Name etc.

Friday, October 14, 2011

Silverlight DataGrid custom style desynchronized header and body columns; Silvelight DataGrid out of place

The problem lies in editing DataGrid style. When you wrap:

<sdk:DataGridCellsPresenter  Name="CellsPresenter"  />

with a border the header in not synchronized with the body of the grid anymore. The simple solution to this problem is to move:

sdk:DataGridFrozenGrid.IsFrozen="True"

from the Cells presenter to the most outer element you wrap the grid body with. Complete solution with Grid border looks like this:

<!-- CellPresenter content part -->
<Border BorderBrush="Black" BorderThickness="0.25" Controls:Grid.Column="1" sdk:DataGridFrozenGrid.IsFrozen="True">
    <sdk:DataGridCellsPresenter  Name="CellsPresenter"  />
</Border>

Tuesday, October 11, 2011

Silverlight CustomValidation attribute; ValidationResult.MemberNames.Count == 0

When writing CustomValidation remember to include memberNames in ValidationResult. ValidationResult.MemberNames include property names which are validated at a given moment (are connected to the validation result which we received).

Sample validation should look like the following:

public class CustomValidationMethods
{
    public static ValidationResult SthNumberValidation(string name, ValidationContext validationContext)
    {
        string[] memberNames = new string[] { validationContext.MemberName };

        if (name.StartsWith("B"))
            return ValidationResult.Success;
        else
            return new ValidationResult("Name does not begin with 'B'", memberNames);
    }
}

Silverlight System.Windows.Controls.ComboBox System.InvalidOperationException: Element is already the child of another element

Never try to use elements of Items collection of ComboBox directly and add them to a GRID like this:

SomeCellContainer.Add(SomeComboItems[iter]);

Everything works fine until you need to change the selected Item. When you press on the ComboBox (Silverlight behaviour) the application crashes with System.InvalidOperationException: Element is already the child of another element.

Thursday, October 6, 2011

Silverlight Hierarchical DataGrid; TreeList DataGrid in pure Silverlight

The purpose of this sample code is to create a DataGrid which rows can be expanded. In others words a combination of DataGrid and a TreeList.

Original post is available here:

http://blogs.perpetuumsoft.com/silverlight/how-to-turn-silverlight-datagrid-to-treegrid-in-15-minutes/

I just converted the project from VS2008 to VS2010&SL4:

http://sites.google.com/site/bkosarzyckistorage1/HierarchicalGridDemoVisualStudio2010.7z