【问题标题】:Silverlight add datagrid column with usercontrol using xamlreaderSilverlight 使用 xamlreader 添加带有用户控件的 datagrid 列
【发布时间】:2009-03-23 14:35:22
【问题描述】:

我尝试使用以下代码向我的数据网格添加一列,但我会遇到浏览器崩溃(看不到异常)。我在这里添加了一个示例项目:http://cid-5d0909fd6cd506a0.skydrive.live.com/self.aspx/Offentlig/DependencyPropblem.zip

代码如下:

 private void LoadDynamicDataColumnTemplate()
    {

        string xaml;
        if (false)
        {
            // THis one does not work. Whats wrong?????????
            xaml = @"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007"" 
                               xmlns:me=""clr-namespace:SilverlightApplication2"">
                                              <me:MyUserControl Age=""{Binding Path=Age}""/>
                                        </DataTemplate>";
        }
        else
        {
            // This runs fine!
            xaml = @"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007"" 
                               xmlns:me=""clr-namespace:SilverlightApplication2"">
                                  <Button Content=""{Binding Path=Age}""/>
                            </DataTemplate>";
        }

        DataGridTemplateColumn tc = new DataGridTemplateColumn();
        tc.CellTemplate = (DataTemplate)XamlReader.Load(xaml);
        tc.Header = "Dynamic";
        MyDataGrid.Columns.Add(tc);
    }

我的数据网格是这样定义的:

        <data:DataGrid x:Name="MyDataGrid" ItemsSource="{Binding Path=Rows}" AutoGenerateColumns="False">
        <data:DataGrid.Columns>
            <data:DataGridTextColumn Binding="{Binding Path=Name}" Header="Name"></data:DataGridTextColumn>
            <data:DataGridTemplateColumn Header="Static">
                <data:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <me:MyUserControl Age="{Binding Path=Age}"/>
                    </DataTemplate>
                </data:DataGridTemplateColumn.CellTemplate>
            </data:DataGridTemplateColumn>
        </data:DataGrid.Columns>
    </data:DataGrid>

我的用户控件是这样的:

<UserControl x:Class="SilverlightApplication2.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
>
<Grid x:Name="LayoutRoot" Background="Red">
    <Button Content="{Binding Age}" Click="Button_Click"/>
</Grid>

任何帮助都会很棒!

我正在使用 SL 3 beta 运行,但在卸载它并重新安装工具后,我设法得到了一个异常:

Sys.InvalidOperationException:控件“Xaml1”中的 ManagedRuntimeError 错误 #4004:System.Windows.Markup.XamlParseException:System.Windows.Markup.XamlParseException:System.Windows.Markup.XamlParseException:System.Windows.Markup.XamlParseException:System .Windows.Markup.XamlParseException:System.Windows.Markup.XamlParseException:System.Windows.Markup.XamlParseException:System.Windows.Markup.XamlParseException:System.Windows.Markup.XamlParseException:System.Windows.Markup.XamlParseException:System.Windows .Markup.XamlParseException:AG_E_UNKNOWN_ERROR [行:1 位置:162] 在 MS.Internal.XcpImports.MethodEx(IntPtr ptr,字符串名称,CValue[] cvData) 在 MS.Internal.XcpImports.MethodEx(DependencyObject obj,字符串名称) 在 MS.Internal.XcpImports.DataTemplate_LoadContent(DataTemplate 模板) 在 System.Windows.DataTemplate.LoadContent() 在 System.Windows.Controls.DataGrid.PopulateCellContent(布尔 forceTemplating,布尔 isCellEdited,DataGridColumn dataGridColumn,DataGridRow dataGridRow,DataGridCell dataGridCell) 在 System.Windows.Controls.DataGrid.AddNewCellPrivate(DataGridRow 行,DataGridColumn 列) 在 System.Windows.Controls.DataGrid.CompleteCellsCollection(DataGridRow dataGridRow) 在 System.Windows.Controls.DataGrid.GenerateRow(Int32 rowIndex) 在 System.Windows.Controls.DataGrid.GetEdgedExactRowHeight(Int32 rowIndex) 在 System.Windows.Controls.DataGrid.UpdateDisplayedRows(Int32 newFirstDisplayedRowIndex,双显示高度) 在 System.Windows.Controls.DataGrid.ComputeScrollBarsLayout() 在 System.Windows.Controls.DataGrid.MeasureOverride(大小可用大小) 在 System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget,Single inWidth,Single inHeight,Single& outWidth,Single& outHeight)[行:0 位置:0] 在 MS.Internal.XcpImports.MethodEx(IntPtr ptr,字符串名称,CValue[] cvData) 在 MS.Internal.XcpImports.MethodEx(DependencyObject obj,字符串名称) 在 MS.Internal.XcpImports.DataTemplate_LoadContent(DataTemplate 模板)

但仍然没有运行代码。现在我使用的是 2.0.40115.0 和 VSTS 2008 Dev ed。

有什么想法吗?

问候拉尔西

【问题讨论】:

  • 我在 VSTS 2008 SP1 上运行,安装了 SL 3.0 Beta .....
  • 好的,如果我有时间我今晚会在 SL 3.0 上尝试,我手头没有。

标签: silverlight datagrid


【解决方案1】:

我尝试了您的示例,当将“ if (false)”更改为“ if (true)”时,它在我的机器上运行良好

您使用的是什么版本的 SL、VS 等?

好的,这是您的问题: 在定义“动态”模板的字符串中,您指出

xmlns:me=""clr-namespace:SilverlightApplication2""

在 XamlLoader 的上下文中,您需要指出实际的程序集,因为它没有被编译(我相信这是在 xaml 文件中使用时在编译时替换的快捷方式)。

无论如何,无论是什么根本原因,您都需要说明:

xmlns:me=""clr-namespace:SilverlightApplication2;assembly=SilverlightApplication2""

这给了你

xaml = @"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007"" 
              xmlns:me=""clr-namespace:SilverlightApplication2;assembly=SilverlightApplication2"">
              <me:MyUserControl Age=""{Binding Path=Age}""/>
         </DataTemplate>";

这段代码实际上可以在我的 SL 3.0 机器上运行。

【讨论】:

  • 嗨!谢谢回答。我已经用版本号更新了帖子。
  • 我为此头疼了很长时间 - 非常感谢您帮助我!
猜你喜欢
  • 1970-01-01
  • 2011-10-13
  • 1970-01-01
  • 2012-06-27
  • 1970-01-01
  • 2011-08-20
  • 2011-09-30
  • 1970-01-01
  • 2010-11-02
相关资源
最近更新 更多