DependencyPropert is Null

  • WPF

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von Artentus.

    DependencyPropert is Null

    Hi

    also ich bin ratlos. Ich möchte einem UserControl, welches in Xaml(in dem HauptWidow) "erzeugt" wird ein Property mitgeben. Also habe ich ein dependencyProperty hinzugefügt. Was er an das DependencyProperty binden soll: Ein Property aus dem DataContext in dem "HaupWindow". Das Property bleibt aber Null

    Hier mal ein wenig code
    Spoiler anzeigen

    Quellcode

    1. <TabItem Name="ClickAndMoveTab">
    2. <local:ClickAndMove DatenErfassung="{Binding Path=DataContext.Daten_ErfassungProperty, ElementName=HauptWindow ,diag:PresentationTraceSources.TraceLevel=High, Converter={StaticResource DebugBinding} }" >
    3. </local:ClickAndMove>
    4. </TabItem>


    Spoiler anzeigen

    Quellcode

    1. Public Shared DatenErfassungProperty As DependencyProperty = DependencyProperty.Register("DatenErfassung", GetType(Daten_Erfassung), GetType(ClickAndMove))
    2. Public Property DatenErfassung As Daten_Erfassung
    3. Get
    4. Return _Daten_Erfassung
    5. End Get
    6. Set(value As Daten_Erfassung)
    7. _Daten_Erfassung = value
    8. End Set
    9. End Property


    Hier mal was mir VS ausgibt:
    Spoiler anzeigen
    System.Windows.Data Warning: 56 : Created BindingExpression (hash=26391766) for Binding (hash=53941667)
    System.Windows.Data Warning: 58 : Path: 'DataContext.Daten_ErfassungProperty'
    System.Windows.Data Warning: 60 : BindingExpression (hash=26391766): Default mode resolved to OneWay
    System.Windows.Data Warning: 61 : BindingExpression (hash=26391766): Default update trigger resolved to PropertyChanged
    System.Windows.Data Warning: 62 : BindingExpression (hash=26391766): Attach to MyoDat.ClickAndMove.DatenErfassung (hash=25759335)
    System.Windows.Data Warning: 67 : BindingExpression (hash=26391766): Resolving source
    System.Windows.Data Warning: 70 : BindingExpression (hash=26391766): Found data context element: <null> (OK)
    System.Windows.Data Warning: 74 : Lookup name HauptWindow: queried TabItem (hash=32377341)
    System.Windows.Data Warning: 78 : BindingExpression (hash=26391766): Activate with root item Window2 (hash=57110959)
    System.Windows.Data Warning: 108 : BindingExpression (hash=26391766): At level 0 - for Window2.DataContext found accessor DependencyProperty(DataContext)
    System.Windows.Data Warning: 104 : BindingExpression (hash=26391766): Replace item at level 0 with Window2 (hash=57110959), using accessor DependencyProperty(DataContext)
    System.Windows.Data Warning: 101 : BindingExpression (hash=26391766): GetValue at level 0 from Window2 (hash=57110959) using DependencyProperty(DataContext): <null>
    System.Windows.Data Warning: 106 : BindingExpression (hash=26391766): Item at level 1 is null - no accessor
    System.Windows.Data Warning: 80 : BindingExpression (hash=26391766): TransferValue - got raw value {DependencyProperty.UnsetValue}
    System.Windows.Data Warning: 88 : BindingExpression (hash=26391766): TransferValue - using fallback/default value <null>
    System.Windows.Data Warning: 89 : BindingExpression (hash=26391766): TransferValue - using final value <null>
    System.Windows.Data Warning: 96 : BindingExpression (hash=26391766): Got PropertyChanged event from Window2 (hash=57110959) for DataContext
    System.Windows.Data Warning: 101 : BindingExpression (hash=26391766): GetValue at level 0 from Window2 (hash=57110959) using DependencyProperty(DataContext): MainViewModel (hash=23519281)
    System.Windows.Data Warning: 108 : BindingExpression (hash=26391766): At level 1 - for MainViewModel.Daten_ErfassungProperty found accessor RuntimePropertyInfo(Daten_ErfassungProperty)
    System.Windows.Data Warning: 104 : BindingExpression (hash=26391766): Replace item at level 1 with MainViewModel (hash=23519281), using accessor RuntimePropertyInfo(Daten_ErfassungProperty)
    System.Windows.Data Warning: 101 : BindingExpression (hash=26391766): GetValue at level 1 from MainViewModel (hash=23519281) using RuntimePropertyInfo(Daten_ErfassungProperty): Daten_Erfassung (hash=5588369)
    System.Windows.Data Warning: 80 : BindingExpression (hash=26391766): TransferValue - got raw value Daten_Erfassung (hash=5588369)
    System.Windows.Data Warning: 82 : BindingExpression (hash=26391766): TransferValue - user's converter produced Daten_Erfassung (hash=5588369)
    System.Windows.Data Warning: 89 : BindingExpression (hash=26391766): TransferValue - using final value Daten_Erfassung (hash=5588369)


    Was funktioniert ist:
    Spoiler anzeigen
    Private Sub ClickAndMove_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
    _Daten_Erfassung = DirectCast(Me.Parent, TabItem).DataContext.Daten_ErfassungProperty

    End Sub

    Dies kann aber nicht die Lösung sein, da das Parent des UserControls immer etwas anderes sein kann
    Im Getter und Setter der Property musst du auch die Dependency-Property verwenden, kein Backing-Field, ansonsten hängen die beiden in keinster Weise irgendwie zusammen.

    VB.NET-Quellcode

    1. Public Property DatenErfassung As Daten_Erfassung
    2. Get
    3. Return DirectCast(GetValue(DatenErfassungProperty), Daten_Erfassung)
    4. End Get
    5. Set(value As Daten_Erfassung)
    6. SetValue(DatenErfassungProperty, value)
    7. End Set
    8. End Property

    Außerdem bindest du ziemlich seltsam. Alle Steuerelemente erben den DataContext ihres Parent, wenn kein anderer angegeben wird, also kannst du das viel kürzer lösen:

    XML-Quellcode

    1. <local:ClickAndMove DatenErfassung="{Binding Daten_ErfassungProperty, diag:PresentationTraceSources.TraceLevel=High, Converter={StaticResource DebugBinding} }"/>


    PS: du benennst nicht nach Guideline. ^^
    Kurz erstmal zu der zweiten Teil: Weil in der New Methode Me.DataContext = Me steht. Somit muss ich auf ein anderes Control zugreifen

    Zum ersten: Danke. Aber ich greife mehrmals auf die DatenERfassungs-klasse zu. Ich kann ja nicht immer DirectCast(GetValue(DatenErfassungProperty), Daten_Erfassung) schreiben. Wieso funktioniert denn
    Spoiler anzeigen

    Quellcode

    1. Public Property DatenErfassung As Daten_Erfassung
    2. Get
    3. Return DirectCast(GetValue(DatenErfassungProperty), Daten_Erfassung)
    4. End Get
    5. Set(value As Daten_Erfassung)
    6. SetValue(DatenErfassungProperty, value)
    7. _Daten_Erfassung = DirectCast(GetValue(DatenErfassungProperty), Daten_Erfassung)
    8. End Set
    9. End Property
    nicht?

    Wenn ich in der loaded methode _Daten_Erfassung = DirectCast(GetValue(DatenErfassungProperty), Daten_Erfassung) schreibe funktioniert es

    Eggord schrieb:

    Weil in der New Methode Me.DataContext = Me steht.
    Aua! So ists eigentlich nicht gedacht, das macht dir das MVVM-Pattern komplett zunichte. Leg eine separate Viewmodel-Klasse an und weise sie per XAML zu, dann funktioniert auch sowas hier:
    MVVM: "Binding-Picking" im Xaml-Editor

    Eggord schrieb:

    Ich kann ja nicht immer DirectCast(GetValue(DatenErfassungProperty), Daten_Erfassung) schreiben.
    Musst du ja auch nicht, genau dafür ist die Wrapper-Property ja da.
    Dein Code funktioniert nicht, da der Setter nie aufgerufen wird und dementsprechend auch das Backing-Field nie gesetzt wird. Die Bindings im XAML setzen die Werte direkt mit SetValue, das gezeigte Pattern hat schon so seine Richtigkeit.