我有问题,具有约束力。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中以红色突出显示并出现错误。
哪里是错误?
Hamlets anser类作品。我没有得到这个错误,我可以在snoop中看到画布轮廓,但没有显示椭圆。 – Hooch
@Hooch:你也应该改变'Path'的'Stroke'属性。我必须检查一下 - 我对自己的视觉树概念感到困惑:p,ps,绑定是否真的读取了“Position”属性?在吸气剂上放置断点。如果绑定真的有效,那就是可靠的方法。 –
我发现我的绑定与转换器不起作用。我的转换器返回System.Media.Colors。如果我用手工颜色,它的作品。 – Hooch