定义 DataGridTemplateColumn.Header 导致 "Specified element is already the logical child of another element. Disconnect it first" 异常
Defining DataGridTemplateColumn.Header is causing "Specified element is already the logical child of another element. Disconnect it first" exception
我有一个 MVVM WPF 应用程序。此应用程序有两个数据网格。
其中之一有一个 DataGridTemplateColumn 等等。当我重新加载 UI 时,此 DataGridTemplateColumn 导致异常。在错误的屏幕截图下方(注意:如果我删除此 DataGridTemplateColumn,则一切正常,不会引发异常):
它是西班牙语的。英文是:"Specified element is already the logical child of another element. Disconnect it first"
我发现罪魁祸首是当我通过这样做定义 DataGridTemplateColumn 的 header 时:
<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>
如果我删除上面的 DataGridTemplateColumn.Header 部分,那么它可以工作并且不会引发异常。为什么?如何解决?
通过将项目列表分配给 ItemsSource 数据网格来初始化 DataGrid 属性。
DataGrid 资源是:
<!-- DataGrid Resources -->
<dg:DataGrid.Resources>
<proxy:BindingProxy x:Key="myProxy" Data="{Binding}" />
</dg:DataGrid.Resources>
代理 class 是:
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));
}
DataGridTemplateColumn 如下所示。基本上它包含一个 link ,单击它时它会调用带有参数的命令。该参数是同一个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>
我已经解决了。我改变了定义 DataGridTemplateColumn header 的方式:
<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.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>
现在它就像一个魅力!
我找到了 Frydex 提出的解决方案here。
我有一个 MVVM WPF 应用程序。此应用程序有两个数据网格。
其中之一有一个 DataGridTemplateColumn 等等。当我重新加载 UI 时,此 DataGridTemplateColumn 导致异常。在错误的屏幕截图下方(注意:如果我删除此 DataGridTemplateColumn,则一切正常,不会引发异常):
它是西班牙语的。英文是:"Specified element is already the logical child of another element. Disconnect it first"
我发现罪魁祸首是当我通过这样做定义 DataGridTemplateColumn 的 header 时:
<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>
如果我删除上面的 DataGridTemplateColumn.Header 部分,那么它可以工作并且不会引发异常。为什么?如何解决?
通过将项目列表分配给 ItemsSource 数据网格来初始化 DataGrid 属性。
DataGrid 资源是:
<!-- DataGrid Resources -->
<dg:DataGrid.Resources>
<proxy:BindingProxy x:Key="myProxy" Data="{Binding}" />
</dg:DataGrid.Resources>
代理 class 是:
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));
}
DataGridTemplateColumn 如下所示。基本上它包含一个 link ,单击它时它会调用带有参数的命令。该参数是同一个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>
我已经解决了。我改变了定义 DataGridTemplateColumn header 的方式:
<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.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>
现在它就像一个魅力!
我找到了 Frydex 提出的解决方案here。