[英]WPF Binding Property not found
我有绑定问题。
此行: Center="{Binding Position, RelativeSource={RelativeSource TemplatedParent}}"
在运行时导致问题。 它给了我这个错误:
System.Windows.Data错误:40:BindingExpression路径错误:在“对象”“ ContentPresenter”(名称=“)”上找不到“位置”属性。 BindingExpression:Path = Position; DataItem ='ContentPresenter'(Name =''); 目标元素是'EllipseGeometry'(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中出现错误。
错误在哪里?
{TemplatedParent}
无法正常工作,因为当您从错误中读取内容时,它将解析为ContentPresenter
。 如果您对为什么感兴趣,应该使用Snoop真正检查视觉树。
但是,我认为@Hamlet答案无效。 EllipseGeometry
元素不继承DataContext
。EllipseGeometry
元素EllipseGeometry
可视树中。
您可以尝试以下方法:
Center="{Binding DataContext.Position, RelativeSource={RelativeSource TemplatedParent}}"
为什么绑定受诱惑的父母? 这应该工作。
<Path.Data>
<EllipseGeometry x:Name="PlayerEllipse"
Center="{Binding Position}"
RadiusX="5"
RadiusY="5"/>
</Path.Data>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.