我正在尝试创建一个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>
当我绑定它的属性时,这就像一个魅力。每当我更改组合框时它都会更新内部属性,反之亦然。问题是当我尝试在另一个控件中使用UnitSelectionBox
时,我希望绑定Unit属性。
<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
并将Binding返回到我正在使用的依赖项属性错误:
<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 I添加依赖项属性 IMG with both dependency property
答案 0 :(得分:0)
我刚发现我错误地使用了依赖属性。为了访问属性并绑定它,我应该像这样更改对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方法。