2012-02-17 280 views
0

使用描述的方法here我在嘗試動態構建DataTemplate列。 我有定義我自己的轉換器的問題。 首先問題是定義我自己的節目名稱空間,因爲我得到這個錯誤:在運行時在Silverlight 4.0中定義DataGrid Templatecolumns

類型「RowIndexConverter」沒有被發現,因爲「CLR的命名空間:LANOS.Models; assembly = LANOS'未找到。

根據我基於我的應用程序的解決方案,任何用戶定義的名稱空間還需要程序集名稱。 RowIndexConverter當然屬於LANOS.Models,沒有錯別字。類屬於應用程序的主要程序集(換言之,它不是外部的)。那麼爲什麼會有問題呢?

第二個問題是我不確定我的轉換器的定義是否適合使用這種技術。 我想這等同的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> 

功能代碼。

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(); 
    } 

結果的函數:

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

回答

1

我相信這個問題是在這條線

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

嘗試assembly之前刪除的空間,即:

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

(是的,我真的要求你做一些微不足道的事情,就像去掉一個空間!我很驚訝,這有所作爲。我能夠重現錯誤的空間,但它沒有工作。)

至於你的第二個問題,我沒有看到使用轉換器的問題,因爲你是。我以前做過很多事情。

+0

哦,我現在感覺有點傻了,雖然問題已經解決了,謝謝你的幫助:) – neurotix 2012-02-20 07:20:47

相關問題