使用描述的方法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>
哦,我現在感覺有點傻了,雖然問題已經解決了,謝謝你的幫助:) – neurotix 2012-02-20 07:20:47