WPF错误:无法找到目标元素理事FrameworkElement的 [英] WPF Error: Cannot find governing FrameworkElement for target element
问题描述
我已经得到了使用具有图像的行一个DataGrid。此图片绑定了触发到一定境界。当状态改变我想改变形象。
I've got a datagrid with a Row that has an image. This image is bound with a trigger to a certain state. When the state changes I want to change the image.
模板本身被设置在DataGridTemplateColumn的HeaderStyle。这个模板有一定的绑定。第一个结合天会显示今天是什么日子,国家有触发改变图像。
The Template itself is set on the HeaderStyle of a DataGridTemplateColumn. This template has some bindings. The first binding Day shows what day it is and the State changes the image with a trigger.
这些属性在一个ViewModel设置。
These properties are set in a ViewModel.
属性:
public class HeaderItem
{
public string Day { get; set; }
public ValidationStatus State { get; set; }
}
this.HeaderItems = new ObservableCollection<HeaderItem>();
for (int i = 1; i < 15; i++)
{
this.HeaderItems.Add(new HeaderItem()
{
Day = i.ToString(),
State = ValidationStatus.Nieuw,
});
}
Datagrid的:
Datagrid:
<DataGrid x:Name="PersoneelsPrestatiesDataGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
AutoGenerateColumns="False" SelectionMode="Single" ItemsSource="{Binding CaregiverPerformances}" FrozenColumnCount="1" >
<DataGridTemplateColumn HeaderStyle="{StaticResource headerCenterAlignment}" Header="{Binding HeaderItems[1]}" Width="50">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{ Binding Performances[1].Duration,Converter={StaticResource timeSpanConverter},Mode=TwoWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock TextAlignment="Center" Text="{ Binding Performances[1].Duration,Converter={StaticResource timeSpanConverter}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn> </DataGrid>
Datagrid的HeaderStyleTemplate:
Datagrid HeaderStyleTemplate:
<Style x:Key="headerCenterAlignment"
TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Day}" />
<Image x:Name="imageValidation" Grid.Row="1" Width="16" Height="16" Source="{StaticResource imgBevestigd}" />
</Grid>
<ControlTemplate.Triggers>
<MultiDataTrigger >
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding State}" Value="Nieuw"/>
</MultiDataTrigger.Conditions>
<Setter TargetName="imageValidation" Property="Source" Value="{StaticResource imgGeenStatus}"/>
</MultiDataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
现在,当我启动项目中的图像不显示,我得到这个错误:
Now when I startup the project the images doesn't show and I get this error:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=HeaderItems[0]; DataItem=null; target element is 'DataGridTemplateColumn' (HashCode=26950454); target property is 'Header' (type 'Object')
任何想法,为什么这个错误是显示?感谢名单提前
Any idea why this error is showing? Thanx in advance
推荐答案
不幸的是任何的DataGridColumn
在 DataGrid.Columns
不是视觉
树的一部分,因此没有连接到数据网格的数据上下文。所以绑定不会与他们的性质的工作,如能见度
或标题
等(尽管这些特性都是有效的依赖属性!) 。
Sadly any DataGridColumn
hosted under DataGrid.Columns
is not part of Visual
tree and therefore not connected to the data context of the datagrid. So bindings do not work with their properties such as Visibility
or Header
etc (although these properties are valid dependency properties!).
现在你可能会德豪的是,可能吗?是不是他们的绑定应该被绑定到数据上下文
属性?那么这简直就是一个黑客。绑定不真正发挥作用。它实际上是一个复制 / 克隆此绑定对象,并用它来显示自己的内容的数据网格细胞!
Now you may wonderhow is that possible? Isn't their Binding
property supposed to be bound to the data context? Well it simply is a hack. The binding does not really work. It is actually the datagrid cells that copy / clone this binding object and use it for displaying their own contents!
现在,我们回到解决您的问题,我认为 HeaderItems
是设置为的DataContext $ c中的对象的属性$ C>父视图。我们的可以的通过一些连接
的DataContext
视图以任何的DataGridColumn
的我们称之为< STRONG> ProxyElement
So now back to solving your issue, I assume that HeaderItems
is a property of the object that is set as the DataContext
of your parent View. We can connect the DataContext
of the view to any DataGridColumn
via something we call a ProxyElement
.
下面的例子说明了如何将逻辑子连接,如文本菜单
或的DataGridColumn
父视图的的DataContext
The example below illustrates how to connect a logical child such as ContextMenu
or DataGridColumn
to the parent View's DataContext
<Window x:Class="WpfApplicationMultiThreading.Window5"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vb="http://schemas.microsoft.com/wpf/2008/toolkit"
Title="Window5" Height="300" Width="300" >
<Grid x:Name="MyGrid">
<Grid.Resources>
<FrameworkElement x:Key="ProxyElement" DataContext="{Binding}"/>
</Grid.Resources>
<Grid.DataContext>
<TextBlock Text="Text Column Header" Tag="Tag Columne Header"/>
</Grid.DataContext>
<ContentControl Visibility="Collapsed"
Content="{StaticResource ProxyElement}"/>
<vb:DataGrid AutoGenerateColumns="False" x:Name="MyDataGrid">
<vb:DataGrid.ItemsSource>
<x:Array Type="{x:Type TextBlock}">
<TextBlock Text="1" Tag="1.1"/>
<TextBlock Text="2" Tag="1.2"/>
<TextBlock Text="3" Tag="2.1"/>
<TextBlock Text="4" Tag="2.2"/>
</x:Array>
</vb:DataGrid.ItemsSource>
<vb:DataGrid.Columns>
<vb:DataGridTextColumn
Header="{Binding DataContext.Text,
Source={StaticResource ProxyElement}}"
Binding="{Binding Text}"/>
<vb:DataGridTextColumn
Header="{Binding DataContext.Tag,
Source={StaticResource ProxyElement}}"
Binding="{Binding Tag}"/>
</vb:DataGrid.Columns>
</vb:DataGrid>
</Grid>
</Window>
上面的视图遇到你已经发现,如果我没得砍实施ProxyElement相同的绑定错误。该ProxyElement是,任何FrameworkElement的抢断在的DataContext
从主视图,它提供的逻辑子如文本菜单
或的DataGridColumn
。对于必须举办作为内容
到一个无形的 ContentControl中
这是相同的视野下。
The view above encountered the same binding error that you have found if I did not have implemented the ProxyElement hack. The ProxyElement is any FrameworkElement that steals the DataContext
from the main View and offers it to the logical child such as ContextMenu
or DataGridColumn
. For that it must be hosted as a Content
into an invisible ContentControl
which is under the same View.
我希望这将指导您正确方向。
I hope this guides you in correct direction.
这篇关于WPF错误:无法找到目标元素理事FrameworkElement的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!