【问题标题】:Error binding properties in VB WPFVB WPF 中的错误绑定属性
【发布时间】:2017-11-09 19:03:14
【问题描述】:

我正在尝试创建一个 wpf 库,但有一个问题我找不到解决方案。

首先我有这个控件

<UserControl x:Class="UnitSelectionBox">
[Omitting for size reasons]

<Grid HorizontalAlignment="Stretch"
      VerticalAlignment="Stretch">
    <ComboBox x:Name="UnitList"
              HorizontalAlignment="Stretch"
              VerticalAlignment="Stretch"
              IsReadOnly="True"
              ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                                                                   AncestorType={x:Type local:UnitSelectionBox}},
                             Path=Dimension,
                             Converter={StaticResource converterDimension},
                             UpdateSourceTrigger=PropertyChanged,
                             Mode=OneWay}"
              SelectedItem="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                                                                    AncestorType={x:Type local:UnitSelectionBox}},
                             Path=Unit,
                             Converter={StaticResource converterUnit},
                             UpdateSourceTrigger=PropertyChanged,
                             Mode=TwoWay}"
              IsSynchronizedWithCurrentItem="True"/>
</Grid>

当我绑定它的属性时,这就像一个魅力。每当我更改组合框时,它都会更新内部属性,反之亦然。问题是当我尝试在另一个我想绑定 Unit 属性的控件中使用 UnitSelectionBox 时。

<local:UnitSelectionBox 
    x:Name="UnitBox"
    Grid.Column="1"
    Unit="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:QuantityBox}},
    Path=Unit, 
    Mode=TwoWay, 
    UpdateSourceTrigger=PropertyChanged}">

我收到错误A 'Binding' can only be set on a DependencyProperty of a DependencyObject

我可以通过注册属性来解决这个错误:

Public Shared ReadOnly UnitProperty As DependencyProperty =
    DependencyProperty.Register("Unit",
                                GetType(Unit),
                                GetType(UnitSelectionBox),
                                New PropertyMetadata(Dimensionless.SI.ToUnit))

Public Shared ReadOnly DimensionProperty As DependencyProperty =
    DependencyProperty.Register("Dimension",
                                GetType(IDimension),
                                GetType(UnitSelectionBox),
                                New PropertyMetadata(New Dimensionless))

但是当我这样做时,UnitSelectionBox 停止更新其值。 同样,UnitSelectionBox 在我单独使用时可以工作,但我无法将其属性绑定到另一个控件。如果不是DependencyProperty,我如何在另一个控件中绑定它的属性?


更新:

UnitSelectionBox 中保存这两个属性的代码是

Private P_Unit As Unit = Dimensionless.SI.ToUnit

Property Dimension As IDimension
    Get
        Return P_Unit.Dimension
    End Get
    Set(value As IDimension)
        NotifyPropertyChanging("Dimension")
        Unit = value.SI
        NotifyPropertyChanged("Dimension")
    End Set
End Property

Public Property Unit() As Unit
    Get
        Return P_Unit
    End Get
    Set(ByVal value As Unit)
        NotifyPropertyChanging("Unit")
        P_Unit = value
        NotifyPropertyChanged("Unit")
    End Set
End Property

控件的 XAML 尝试将其属性与 UnitSelectionBox 绑定并返回我正在使用的绑定到依赖项属性错误是:

<local:UnitSelectionBox 
        x:Name="UnitBox"
        Grid.Column="1"
        Unit="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:QuantityBox}},
                       Path=Unit, 
                       Mode=TwoWay, 
                       UpdateSourceTrigger=PropertyChanged}">

我正在使用 WPF 应用程序来测试使用以下代码的控件:

  <ProtoControls:UnitSelectionBox
        Name="UnBox" 
        Dimension="{StaticResource dimensionT}"
        HorizontalAlignment="Left" Height="11" Margin="162,42,0,0" VerticalAlignment="Top" Width="36"/>
    <Button Content="Unit" HorizontalAlignment="Left" Margin="215,42,0,0" VerticalAlignment="Top" Width="75" Click="ButtonUnit_Click"/>

按钮运行的子是:

Private Sub ButtonUnit_Click(sender As Object, e As RoutedEventArgs)
    MsgBox(UnBox.Unit.Name)
End Sub

我期望的结果和不使用 DependencyProperty 时得到的结果是 IMG without dependency property

但是当我为单元添加 DependencyProperty 部分时,我得到 IMG with unit dependency property

为我得到的 Dimension 和 Unit 添加依赖属性 IMG with both dependency property

【问题讨论】:

  • 1) 你是说当你得到错误时,Unit 被定义为常规 C# 属性,而不是依赖属性? 2) 你能否准确地解释一下,当“停止更新它的值”时,你面前的屏幕上有什么不同?请回答这两个问题,谢谢。
  • 3) 你是如何使用UnitDimension inside UserControl 的 XAML 的?你和他们有什么关系吗?
  • @EdPlunkett 我有一个 WPF 应用程序来测试我的控件,这个应用程序有一个按钮可以访问 UnitSelectionBox 的内部属性。因此,当我不使用代码的任何 DependencyProperty 部分时,我有预期的行为:IMG without dependency properties 如果我只删除依赖属性的单位部分,它会使用正确的维度,但不会随着组合框选定项而改变:IMG with unit dependency property 最后,如果我同时使用两者,除了初始值之外,我无法得到任何结果:IMG using both
  • 好的,你有一个盒子,上面写着“Kelvin”。这与您的任何代码或您的任何问题有什么关系?我还没有看到你的其余代码。 请同时回答其他两个问题。
  • @EdPlunkett 是的,当我删除代码的 DependencyProperty 部分时,控件的行为符合我的预期,但我无法将其属性绑定到另一个控件中。

标签: wpf vb.net dependency-properties


【解决方案1】:

我刚刚发现我错误地使用了依赖属性。为了访问属性并绑定它,我应该像这样更改对 Unit 和 Dimension 属性的访问。

Property Dimension As IDimension
    Get
        Return GetValue(UnitProperty).Dimension
    End Get
    Set(value As IDimension)
        NotifyPropertyChanging("Dimension")
        SetValue(UnitProperty, value.SI)
        SetValue(DimensionProperty, value)
        NotifyPropertyChanged("Dimension")
    End Set
End Property

Public Property Unit() As Unit
    Get
        Return GetValue(UnitProperty)
    End Get
    Set(ByVal value As Unit)
        NotifyPropertyChanging("Unit")
        SetValue(UnitProperty, value)
        NotifyPropertyChanged("Unit")
    End Set
End Property

使用依赖对象定义的 GetValue 和 SetValue 方法而不是内部字段。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多