【问题标题】:Defining DataGrid Templatecolumns at runtime in Silverlight 4.0在 Silverlight 4.0 中在运行时定义 DataGrid 模板列
【发布时间】:2012-02-17 14:09:26
【问题描述】:

使用here 描述的方法,我正在尝试动态构建列DataTemplate。 我在定义自己的转换器时遇到了问题。 第一个问题是定义我自己的程序名称空间,因为我遇到了这个错误:

找不到类型“RowIndexConverter”,因为 'clr-namespace: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>

【问题讨论】:

    标签: c# silverlight xaml datagrid


    【解决方案1】:

    我相信问题出在这一行

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

    尝试删除assembly之前的空格,即:

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

    (是的,我真的要求你做一些微不足道的事情,比如删除一个空格!我很惊讶这会产生影响。我能够用空格重现错误,但它在没有空格的情况下也能正常工作。)

    至于您的第二个问题,我认为您使用转换器没有问题。我以前做过很多相同的事情。

    【讨论】:

    • 哦,虽然问题已经解决,但我现在觉得有点傻,谢谢你的帮助:)
    猜你喜欢
    • 2010-11-02
    • 2011-05-13
    • 1970-01-01
    • 2011-02-25
    • 2011-01-02
    • 2011-02-15
    • 2014-01-06
    • 2011-06-17
    • 1970-01-01
    相关资源
    最近更新 更多