【发布时间】:2014-12-28 20:50:30
【问题描述】:
我遇到了绑定问题。
这一行:Center="{Binding Position, RelativeSource={RelativeSource TemplatedParent}}"
在运行时导致问题。它给了我这个错误:
System.Windows.Data 错误:40:BindingExpression 路径错误: 在“对象”“ContentPresenter”上找不到“位置”属性 (名称='')'。绑定表达式:路径=位置; DataItem='ContentPresenter' (Name='');目标元素是 '椭圆几何' (HashCode=63639374);目标属性是“中心” (输入“点”)
这是我的模型:
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
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:CSGOHack.GUI"
x:Class="Game.GUI.MainWindow"
Title="Game Tool" Height="334" Width="415"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<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>
在 Snoop 2.8.0 中,我可以看到其余内容已正确绑定。值转换器正在工作。在 Snoop 中,只有这个“位置”属性以红色突出显示,并带有错误。
哪里出错了?
【问题讨论】:
标签: c# wpf templates binding itemscontrol