使用描述的方法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