2014-12-28 86 views
0

我有问题,具有约束力。WPF绑定属性未找到

This line:Center =“{Binding Position,RelativeSource = {RelativeSource TemplatedParent}}” 会导致运行时出现问题。它给我的错误:

System.Windows.Data Error: 40 : BindingExpression path error: 'Position' property not found on 'object' ''ContentPresenter' (Name='')'. BindingExpression:Path=Position; DataItem='ContentPresenter' (Name=''); target element is 'EllipseGeometry' (HashCode=63639374); target property is 'Center' (type 'Point')

这是我的模型:

public interface IRadarReader 
{ 
    BindingList<RadarEntity> Entities { get; } 
    RadarEntity LocalPlayer { get; } 
    bool Enabled { get; set; } 
} 

public class RadarEntity 
{ 
    public Point Position { get; set; } 
    public PlayerTeam Team { get; set; } 
    public EntityType Type { get; set; } 
} 

我使用System.Windows.Point的位置。

<Window.Resources> 
    <l:RadarTeamToColorConverter x:Key="RadarTeamToColorConverter"/> 
</Window.Resources> 

<Grid> 
    <GroupBox Header="Radar"> 
     <Viewbox> 
      <ItemsControl ItemsSource="{Binding GameReader.RadarReader.Entities}" Background="#FFA4D16E"> 
       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <Canvas Width="100" Height="100"/> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
       <ItemsControl.ItemTemplate> 
        <DataTemplate> 
         <Path Fill="{Binding Team, Converter={StaticResource RadarTeamToColorConverter}}"> 
          <Path.Data> 
           <EllipseGeometry x:Name="PlayerEllipse" 
            Center="{Binding Position, RelativeSource={RelativeSource TemplatedParent}}" 
            RadiusX="5" 
            RadiusY="5"/> 
          </Path.Data> 
         </Path> 
        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 
     </Viewbox> 
    </GroupBox> 
</Grid> 

探听2.8.0我能看到的东西,其余的都正确绑定。价值转换器正在工作。只有这个“位置”属性在Snoop中以红色突出显示并出现错误。

哪里是错误?

回答

2

{TemplatedParent}不会工作,因为当你从错误读取解析为ContentPresenter。如果你有兴趣为什么,你应该用Snoop真正检查视觉树。

不过,我认为doub't答案@Hamlet工作。 EllipseGeometry元素不会继承DataContextEllipseGeometry元素不在视觉树中。

你可以试试这个:

Center="{Binding DataContext.Position, RelativeSource={RelativeSource TemplatedParent}}" 
+0

Hamlets anser类作品。我没有得到这个错误,我可以在snoop中看到画布轮廓,但没有显示椭圆。 – Hooch

+0

@Hooch:你也应该改变'Path'的'Stroke'属性。我必须检查一下 - 我对自己的视觉树概念感到困惑:p,ps,绑定是否真的读取了“Position”属性?在吸气剂上放置断点。如果绑定真的有效,那就是可靠的方法。 –

+0

我发现我的绑定与转换器不起作用。我的转换器返回System.Media.Colors。如果我用手工颜色,它的作品。 – Hooch

1

为什么你绑定到temptated父?这应该工作。

<Path.Data> 
    <EllipseGeometry x:Name="PlayerEllipse" 
       Center="{Binding Position}" 
       RadiusX="5" 
       RadiusY="5"/> 
</Path.Data> 
+0

这是不是现在给我的错误。我将它添加为“可能的”修复程序。谢谢,我再也没有收到这个错误。但由于某种原因没有显示椭圆。 – Hooch