【发布时间】: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