代码之家  ›  专栏  ›  技术社区  ›  Gforse

在“object”上找不到绑定错误属性

  •  0
  • Gforse  · 技术社区  · 6 年前

    我被一个断裂的绑定卡住了,通常这种方法可以工作,但现在我不能让它工作。很简单,我有一个带有椭圆的用户控件,这个椭圆必须根据boolean类型的dependencProperty更改颜色。然而,它似乎没有找到家属的财产。找了几个小时尝试了很多不同的方法,但都没能成功。我想是因为今天是星期五吧?

    XAML:

    <UserControl
        x:Class="NTP_Status"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        d:DesignHeight="100"
        d:DesignWidth="100"
        mc:Ignorable="d">
        <Ellipse
            Width="100"
            Height="100"
            StrokeThickness="2">
            <Ellipse.Style>
                <Style TargetType="{x:Type Ellipse}">
                    <Setter Property="Fill" Value="Red" />
                    <Setter Property="Stroke" Value="Black" />
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Fill" Value="Orange" />
                            <Setter Property="Stroke" Value="Navy" />
                        </Trigger>
                        <DataTrigger Binding="{Binding Path=IsSyncing, RelativeSource={RelativeSource Self}}" Value="True">
                            <Setter Property="Fill" Value="Green" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=IsSyncing, RelativeSource={RelativeSource Self}}" Value="False">
                            <Setter Property="Fill" Value="DarkGreen" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Ellipse.Style>
        </Ellipse>
    </UserControl>
    

    VB.net:

    Public Class NTP_Status
    
        Private Shared ReadOnly IsSyncingProperty As DependencyProperty = DependencyProperty.Register("IsSyncing", GetType(Boolean), GetType(NTP_Status))
    
        Public Property IsSyncing As Boolean
            Get
                Return CBool(Me.GetValue(IsSyncingProperty))
            End Get
    
            Set(ByVal value As Boolean)
                Me.SetValue(IsSyncingProperty, value)
            End Set
        End Property
    End Class
    
    2 回复  |  直到 6 年前
        1
  •  3
  •   ASh aminescm    6 年前

    你有椭圆的样式。绑定 {RelativeSource Self} 意味着绑定将尝试查找属性 IsSyncing 在椭圆对象中,失败的原因是 同步 在用户控件中声明。

    使用 RelativeSource AncestorType

    Binding="{Binding Path=IsSyncing, RelativeSource={RelativeSource AncestorType=UserControl}}"
    

    或者给用户控件命名( x:Name="myControl" )并使用elementname:

    Binding="{Binding Path=IsSyncing, ElementName=myControl}"
    

    Trigger Property="IsMouseOver" Value="true" 应该是最后一个,否则用于ISsyncing的数据触发器将始终覆盖它

        2
  •  1
  •   Olaru Mircea    6 年前

    如果你正在使用 {RelativeSource Self} 椭圆上应该有 同步 椭圆控件的属性。

    当然不是这样,我认为你应该利用这条路= 数据上下文 .issyncing如果ntp_status instance是您的datacontext。

    最好去掉那个相对的源并保持绑定路径现在的样子。

    编辑:

    我知道我被否决了,我不明白为什么,也许是那个做了这件事的人可以解释。

    注意你的 Ellipse 将继承 DataContext 从它的父母那里( UserControl 在本例中)因此不需要在那里使用任何相对资源绑定。