mike
mike

Reputation: 550

Defining DataGrid Templatecolumns at runtime in Silverlight 4.0

Using the method described here I'm trying to dynamically build columns DataTemplate. I've got a problem with defining my own converter. First problem is defining my own program name space because I'm getting this error:

The type 'RowIndexConverter' was not found because 'clr-namespace:LANOS.Models; assembly=LANOS' was not found.

According to the solution I've based my application on, any user defined name space also requires assembly name. RowIndexConverter belongs LANOS.Models for sure, there is no typo. Class belongs to main assembly of application (in the other words its not external). So why is there a problem then?

Second problem is that I'm not sure if my definition of converter is proper using this technique. I want to get equivalent of this UserControl.Resources:

<navigation:Page xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"  x:Class="LANOS.Views.SRFEditor" 
           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"
           xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           xmlns:model="clr-namespace:LANOS.Models"
           mc:Ignorable="d"
           d:DesignWidth="640" d:DesignHeight="480"
           Title="SRFEditotr Page">

<UserControl.Resources>
    <model:RowIndexConverter x:Key="rowIndexConverter"/>

    <DataTemplate x:Key="myCellTemplate">
        <TextBlock Text='{Binding Data, Mode=TwoWay, Converter={StaticResource rowIndexConverter}}' />
    </DataTemplate>
</UserControl.Resources>

Function code.

    private string GenerateTextColumnXAML(string sortMemberKey)
    {
        StringBuilder CellTemp = new StringBuilder();
        CellTemp.Append("<DataTemplate ");
        CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/");
        CellTemp.Append("2006/xaml/presentation' ");
        CellTemp.Append("xmlns:model='clr-namespace:LANOS.Models; assembly=LANOS' ");
        CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
        CellTemp.Append("xmlns:basics='clr-namespace:System.Windows.Controls;");
        CellTemp.Append("assembly=System.Windows.Controls' >");

        CellTemp.Append("<Grid>");
        CellTemp.Append("<Grid.Resources>");
        CellTemp.Append("<model:RowIndexConverter x:Key='rowIndexConverter' />");
        CellTemp.Append("</Grid.Resources>");

        CellTemp.Append("<TextBlock Text='{Binding Data, Mode=TwoWay, Converter={StaticResource rowIndexConverter}, ConverterParameter=" + sortMemberKey + "}' />");
        CellTemp.Append("</Grid>");
        CellTemp.Append("</DataTemplate>");
        return CellTemp.ToString();
    }

Result of function:

<DataTemplate
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
xmlns:model='clr-namespace:LANOS.Models; assembly=LANOS' 
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' 
xmlns:basics='clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls' >
<Grid>
<Grid.Resources><model:RowIndexConverter x:Key='rowIndexConverter' />
</Grid.Resources><TextBlock Text='{Binding Data, Mode=TwoWay, Converter={StaticResource rowIndexConverter}, ConverterParameter=GRID_ROW_ID}' />
</Grid>
</DataTemplate>

Upvotes: 0

Views: 579

Answers (1)

Luke Woodward
Luke Woodward

Reputation: 64949

I believe the problem is in this line

    CellTemp.Append("xmlns:model='clr-namespace:LANOS.Models; assembly=LANOS' ");

Try removing the space before assembly, i.e.:

    CellTemp.Append("xmlns:model='clr-namespace:LANOS.Models;assembly=LANOS' ");

(Yes, I really am asking you to do something as trivial as removing a single space! I'm surprised that this makes a difference. I was able to reproduce the error with the space, yet it worked without.)

As for your second question, I don't see a problem with using a converter as you are. I've done much the same before.

Upvotes: 1

Related Questions