Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
130
xamDiagram using data binding, running into errors with TwoWay binding in diagram items
posted

I have a xamDiagram whose diagram items are populated from a view model using TwoWay data binding, as per the XamDiagram documentation.

For the most part, this works as intended, in that I can modify the diagram item objects in the view model or their respective objects in the diagram, and the TwoWay binding ensures that changes save in both directions. However, there is a really strange case in which the data binding will fail when updating the data in the view model and throw an exception when calling NotifyPropertyChanged on the object.

Note: My data objects in the view model contain most of the same properties that exist on the DiagramItem/Node/Connections in the diagram (e.g., Fill, Position, Stroke, Width, Height, ShapeType)

The exception generally looks like this:

Exception thrown: 'System.Collections.Generic.KeyNotFoundException' in mscorlib.dll
Exception thrown: 'System.Reflection.TargetInvocationException' in mscorlib.dll
System.Windows.Data Error: 8 : Cannot save value from target back to source. BindingExpression:Path=Content.Position; DataItem='DiagramNode' (Name=''); target element is 'DiagramNode' (Name=''); target property is 'Position' (type 'Point') KeyNotFoundException:'System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at Infragistics.Controls.Charts.XamDiagram.PropertyChangedWeakCollectionChanged(IFastItemsSource fastItemsSource, Object sender, PropertyChangedEventArgs args)
at Infragistics.ItemSourceEventProxy.propertyChanged(Object sender, PropertyChangedEventArgs e)
at System.ComponentModel.PropertyChangedEventHandler.Invoke(Object sender, PropertyChangedEventArgs e)
at GalaSoft.MvvmLight.ObservableObject.RaisePropertyChanged(String propertyName)
at Sandbox.Custom.SandboxSketchDiagram.Items.SketchDiagramNode.set_Position(Point value) in C:\Users\afink\Sandbox\Sandbox\Custom\SandboxSketchDiagram\Items\SketchDiagramNode.cs:line 26'

where SketchDiagramNode.cs:line 26 is where NotifyPropertyChanged() is being executed in the object being updated in the view model. Again, this only happens occasionally.

Also, here is what the data binding looks like for the items in the ItemsSource collection in my diagram:

<Style x:Key="DiagramItemStyle" 
               TargetType="ig:DiagramItem">
            <Setter Property="Fill" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Fill, Mode=TwoWay}" />
            <Setter Property="Visibility" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Visibility, Mode=TwoWay}" />
            <Setter Property="IsEnabled" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Visibility, Converter={StaticResource ResourceKey=VisibilityToBoolConverter}}" />
            <Setter Property="Stroke" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Stroke, Mode=TwoWay}" />
            <Setter Property="StrokeThickness" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.StrokeThickness, Mode=TwoWay}" />
            <Setter Property="StrokeDashArray" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.StrokeDashArray, Mode=TwoWay}" />
            <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Foreground, Mode=TwoWay}" />
            <Setter Property="FontFamily" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.FontFamily, Mode=TwoWay}" />
            <Setter Property="FontSize" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.FontSize, Mode=TwoWay}" />
            <Setter Property="FontStyle" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.FontStyle, Mode=TwoWay}" />
            <Setter Property="FontWeight" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.FontWeight, Mode=TwoWay}" />
            <Setter Property="Opacity" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Opacity, Mode=TwoWay}" />
            <Setter Property="ZIndex" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.ZIndex, Mode=TwoWay}" />
        </Style>

        <Style x:Key="DiagramNodeStyle"
               TargetType="ig:DiagramNode"
               BasedOn="{StaticResource ResourceKey=DiagramItemStyle}">
            <Setter Property="Position" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Position, Mode=TwoWay}" />
            <Setter Property="ShapeType" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.ShapeType, Mode=TwoWay}" />
            <Setter Property="Width" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Width, Mode=TwoWay}" />
            <Setter Property="Height" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Height, Mode=TwoWay}" />
            <Setter Property="MaintainAspectRatio" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.MaintainAspectRatio, Mode=TwoWay}" />
        </Style>

I have tried to create a minimal reproducible example project to recreate the error, but I have yet to succeed in reproducing the error on a new test project, so I still don't know what is causing it or what I may be doing wrong. 

I know this could be pretty difficult to diagnose without any kind of example code to reproduce it, but I was wondering if you guys knew of anything that I could be generally doing wrong when using two-way binding on the ItemsSource collection in the xamDiagram that could cause this exception to be thrown?

Parents
  • 34430
    Offline posted

    Hello Andrew,

    I have been investigating into the behavior you are seeing, and I have a few questions for you on this matter. Would it be possible for you to please provide some information on the following?

    1. What is the action that you or the user is taking when this exception occurs? By chance is it while moving a node? I ask, as I see a binding issue in the trace you provided complaining about the binding to Content.Position of the DiagramNode.

    2. What are you currently binding the Position of your node to?

    3. Would it be possible for you to please attach the “minimal reproducible example project” that you created to try to reproduce the behavior here?

    Please let me know if you have any other questions or concerns on this matter.

Reply Children