简体   繁体   中英

WPF Binding Property not found

I have problem with binding.

This line: Center="{Binding Position, RelativeSource={RelativeSource TemplatedParent}}" causes a problem at runtime. It gives me this error:

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')

This is my model:

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; }
}

I'm using System.Windows.Point for Position.

<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>

In Snoop 2.8.0 I can see that rest of things are binded correctly. Value converter is working. Only this "Position" property is highlighted red with errors in Snoop.

Where is error?

The {TemplatedParent} wont work because that resolves to ContentPresenter as you read from the error. If you're interested why, you should really inspect visual tree with Snoop.

However, I doub't that @Hamlet answer works. The EllipseGeometry element does not inherit the DataContext . The EllipseGeometry element is NOT in the visual tree.

You could try this:

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

Why you bind to temptated parent? This should work.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM