【问题标题】:WPF Binding Property not found未找到 WPF 绑定属性
【发布时间】: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


    【解决方案1】:

    {TemplatedParent} 不起作用,因为当您从错误中读取时,它会解析为 ContentPresenter。如果您对原因感兴趣,您应该真正使用 Snoop 检查可视化树。

    但是,我怀疑@Hamlet 的答案是否有效。 EllipseGeometry 元素不继承 DataContextEllipseGeometry 元素不在可视化树中。

    你可以试试这个:

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

    【讨论】:

    • Hamlets anser 类型的作品。我没有收到此错误,我可以在 snoop 中看到画布轮廓,但没有显示椭圆。
    • @Hooch:您也应该更改PathStroke 属性。我必须检查一下 - 我对这个可视树概念感到困惑:p,ps,绑定真的读取了Position 属性吗?在 getter 上放置断点。如果绑定真的有效,这是可靠的方法。
    • 我发现我与转换器的绑定不起作用。我的转换器返回 System.Media.Colors。如果我用手涂上颜色,它会起作用。
    • @Hooch:Converer 应该返回Brush。试试new SolidColorBrush(..)
    • 效果很好。非常感谢。
    【解决方案2】:

    为什么要绑定到受诱惑的父母身上?这应该可以。

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

    【讨论】:

    • 现在没有给我那个错误。我将其添加为“可能的”修复。谢谢,我不再收到此错误。但由于某种原因,没有显示省略号。
    猜你喜欢
    • 1970-01-01
    • 2020-04-19
    • 2021-03-11
    • 2017-04-28
    • 2014-01-14
    • 2015-01-22
    • 2016-12-10
    • 2011-02-01
    • 1970-01-01
    相关资源
    最近更新 更多