[英]Defining DataGridTemplateColumn.Header is causing “Specified element is already the logical child of another element. Disconnect it first” exception
I have an MVVM WPF application. 我有一个MVVM WPF应用程序。 This application has two datagrids.
该应用程序有两个数据网格。
One of them has a DataGridTemplateColumn among others. 其中之一具有DataGridTemplateColumn等。 This DataGridTemplateColumn is causing an exception when I reload UI.
重新加载UI时,此DataGridTemplateColumn导致异常。 Below the screenshot of the error (Note: if I remove this DataGridTemplateColumn then all work ok, no exception is raised):
错误屏幕截图下方(注意:如果我删除此DataGridTemplateColumn,则一切正常,没有异常):
It is in Spanish. 它是西班牙语。 In English is: "Specified element is already the logical child of another element. Disconnect it first"
用英语是:“指定的元素已经是另一个元素的逻辑子代。请先断开连接”
I have detected that the culprit is when I define the header of the DataGridTemplateColumn by doing this: 通过执行以下操作,当我定义DataGridTemplateColumn的标题时,我发现了罪魁祸首:
<dg:DataGridTemplateColumn.Header>
<StackPanel Orientation="Horizontal" >
<Image RenderOptions.BitmapScalingMode="NearestNeighbor"
Width="32" Height="32"
Source="/My.Utilities.Resources;component/Images/MyIcon.png"/>
<TextBlock Text="Show PDF" VerticalAlignment="Center"/>
</StackPanel>
</dg:DataGridTemplateColumn.Header>
If I remove above DataGridTemplateColumn.Header section then it works and no exception is raised. 如果我删除上面的DataGridTemplateColumn.Header部分,则它可以正常工作并且不会引发异常。 Why?
为什么? How to solve this?
如何解决呢?
The DataGrid is initialized by assigning a List of items to ItemsSource datagrid property. 通过将项目列表分配给ItemsSource datagrid属性来初始化DataGrid。
DataGrid Resources are: DataGrid资源是:
<!-- DataGrid Resources -->
<dg:DataGrid.Resources>
<proxy:BindingProxy x:Key="myProxy" Data="{Binding}" />
</dg:DataGrid.Resources>
and the Proxy class is: 代理类是:
public class BindingProxy : Freezable
{
#region Overrides of Freezable
protected override Freezable CreateInstanceCore()
{
return new BindingProxy();
}
#endregion
public object Data
{
get { return (object)GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}
// Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}
The DataGridTemplateColumn is as below. DataGridTemplateColumn如下。 Basically It contains a link that when is clicked it calls to a command with a parameter.
基本上,它包含一个链接,单击该链接时,它将调用带有参数的命令。 This parameter is the pdf file name contained in another column of the same datagrid:
此参数是包含在同一datagrid的另一列中的pdf文件名:
<dg:DataGridTemplateColumn MinWidth="100" Width="auto">
<dg:DataGridTemplateColumn.Header>
<StackPanel Orientation="Horizontal" >
<Image RenderOptions.BitmapScalingMode="NearestNeighbor"
Width="32" Height="32"
Source="/My.Utilities.Resources;component/Images/MyIcon.png"/>
<TextBlock Text="Show PDF" VerticalAlignment="Center"/>
</StackPanel>
</dg:DataGridTemplateColumn.Header>
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center">
<Hyperlink Command="{Binding Path=Data.ShowPdf, Source={StaticResource myProxy}}"
CommandParameter="{Binding Path=FileName}" >
Show PDF
</Hyperlink>
</TextBlock>
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
I have solved it. 我已经解决了 I have changed the way to define the DataGridTemplateColumn header by changing:
我通过以下方式更改了定义DataGridTemplateColumn标头的方式:
<dg:DataGridTemplateColumn.Header>
<StackPanel Orientation="Horizontal" >
<Image RenderOptions.BitmapScalingMode="NearestNeighbor"
Width="32" Height="32"
Source="/My.Utilities.Resources;component/Images/MyIcon.png"/>
<TextBlock Text="Show PDF" VerticalAlignment="Center"/>
</StackPanel>
</dg:DataGridTemplateColumn.Header>
by this one: 通过这个:
<dg:DataGridTemplateColumn.HeaderStyle>
<Style TargetType="dg:DataGridColumnHeader">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<Image RenderOptions.BitmapScalingMode="NearestNeighbor"
Width="32" Height="32" Source="/My.Utilities.Resources;component/Images/MyIcon.png"/>
<TextBlock Text="Show PDF" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</dg:DataGridTemplateColumn.HeaderStyle>
Now it works like a charm! 现在它就像一种魅力!
I have found the solution here proposed by Frydex. 我在这里找到了Frydex提出的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.