2017-11-09 7 views
1

wpfライブラリを作成しようとしていますが、解決策が見つからないという問題があります。VB 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で使用しようとするときです。

<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でない場合、そのプロパティを別のコントロールにバインドする方法はありますか?


UPDATE:

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 

性誤差を依存性UnitSelectionBoxとそのプロパティを結合して結合を返すようにしようと制御のXAMLその私は使用しています:


私は、次のコードを使用してコントロールをテストするために、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"/> 

ボタンで実行subがある:

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

私が期待した結果と1私は、DependencyPropertyには IMG without dependency property

で使用しない場合に取得するしかし、私は単位のためにしたDependencyPropertyの一部を追加するとき、私は を取得します

ディメンションとユニットの両方のための依存関係プロパティを追加する私は、私はちょうど私が間違って依存関係プロパティを使用していることが判明 IMG with both dependency property

+1

1)エラーが発生したとき、 'Unit'は通常のC#プロパティとして定義されています。依存プロパティではありません。 2) "価値の更新を止める"ときに、あなたの目の前の画面で何が違うのかを視覚的に正確に説明できますか?両方の質問にお返事ください、ありがとうございます。 –

+0

3)UserControlのXAMLの内部で、どのように 'Unit'と' Dimension' *を使用していますか?あなたは彼らと何かをやっていますか? –

+0

@EdPlunkettコントロールをテストするWPFアプリケーションがあります。このアプリケーションには、UnitSelectionBoxの内部プロパティにアクセスするボタンがあります。だから私はコードのDependencyProperty部分を使用していないとき、私は期待された動作があります: [依存プロパティなしのIMG](https://ibb.co/jGoMQw) 依存プロパティ正しい次元を使用しますが、コンボボックスの選択項目では変更されません。 [ユニット依存プロパティ付きIMG] 最後に両方を使用すると、初期値: [両方を使用するIMG](https://ibb.co/hZGEdG) –

答えて

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メソッドを使用します。

関連する問題