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=\"\"  "
    + "xmlns:x=\"\" >"
    + "<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:


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"  />

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;
            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:


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:

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