DataContext lost

3 posts, 1 answers
  1. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 01 Aug 2013 Link to this post

    Hi,

    I tried to work with bound Properties on ConnectorShapes.
    This works fine - as Long as I do not clear the InternalItems / InternalLinks in the Graph source.

    First the Style (part of it)
    <Style TargetType="telerik:RadDiagramConnection" >
            <!---->
            <Setter Property="Visibility" Value="{Binding Visibility, Mode=TwoWay}" />
            <!---->

    Now my (simple) classes:
    (forget about INPBase - it's just an implementation of INotifyPropertyChanged
    The Node:
    public class Item : INPBase {
            #region Content
            private string _Content;
            public string Content {
                get { return _Content; }
                set { Set(ref _Content, value); }
            }
            #endregion
        }


    The Link:
    public class Link : INPBase, ILink<Item> {
        public Visibility Visibility { get; set; }
        public Item Source { get; set; }
        public Item Target { get; set; }
     
        object ILink.Source {
            get { return ((object)Source); }
            set { if (!((object)Source).Equals(value)) { Source = value as Item; } }
        }
        object ILink.Target {
            get { return ((object)Target); }
            set { if (!((object)Target).Equals(value)) { Target = value as Item; } }
        }
        public Link() { Visibility = Visibility.Visible; }
    }


    The Tree:
    public class TestTree : INPBase, IGraphSource {
            public ObservableCollection<Item> InternalItems { get; private set; }
            public ObservableCollection<Link> InternalLinks { get; private set; }
            public Item RootNode { get; private set; }
            public System.Collections.IEnumerable Items {           get { return (InternalItems); }     }
            public IEnumerable<ILink> Links {           get { return(InternalLinks); }      }
     
            public TestTree() {
                InternalItems = new ObservableCollection<Item>();
                InternalLinks = new ObservableCollection<Link>();
                BuildTree();
            }
            private void BuildTree() {
                RootNode = new Item() { Content = "One" };
                Item iTwo = new Item() { Content = "Two" };
                Item iThree = new Item() { Content = "Three" };
                Item iFour = new Item() { Content = "Four" };
                InternalItems.Add(RootNode);
                InternalItems.Add(iTwo);
                InternalItems.Add(iThree);
                InternalItems.Add(iFour);
                InternalLinks.Add(new Link() { Target = RootNode, Source = iTwo });
                InternalLinks.Add(new Link() { Target = RootNode, Source = iThree });
                InternalLinks.Add(new Link() { Target = RootNode, Source = iFour });
            }
            public void RebuildTree() {
                InternalLinks.Clear();
                InternalItems.Clear();
                BuildTree();
            }
            public void AddNodeWithLink() {
                Item iNew = new Item() { Content = "I" + InternalItems.Count.ToString() };
                Item iSource = InternalItems.First();
                InternalItems.Add(iNew);
                InternalLinks.Add(new Link() { Target = iSource, Source = iNew });
            }
        }


    Last not Least I have a "ViewModel" like this:
    public class TestVM : INPBase {
        private TestTree _TestTree;
        public TestTree TestTree {
            get { return _TestTree; }
        }
        public TestVM() {
            _TestTree = new TestTree();
        }
        public void AddNode() {
            _TestTree.AddNodeWithLink();
        }
        public void Rebuild() {
            _TestTree.RebuildTree();
        }
     
    }
    (of course in real code things are handled via commands - but that doesn't matter)


    Everything works fine - as long as I'm not resetting the tree.
    After this I get the debug message:

    System.Windows.Data Error: 40 : BindingExpression path error: 'Visibility' property not found on 'object' ''TestVM' (HashCode=61656587)'. BindingExpression:Path=Visibility; DataItem='TestVM' (HashCode=61656587); target element is 'RadDiagramConnection' (Name=''); target property is 'Visibility' (type 'Visibility')


    So it seems as if clearing the collections move the datacontext "on step up".
    My Binding is simple the UserControl (hosting the diagram) has an Instance of TestVM as DataContext and the Diagram is bound like this:
    <Grid DataContext="{Binding}" x:Name="grdHolder">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Button Content="Add node" VerticalAlignment="Center" HorizontalAlignment="Left" Click="AddNode_Click" />
            <Button Content="Rebuild" VerticalAlignment="Center" HorizontalAlignment="Center" Click="Rebuild_Click" />
            <Button Content="Layout" VerticalAlignment="Center" HorizontalAlignment="Right" Click="Layout_Click" />
            <telerik:RadDiagram x:Name="dG" GraphSource="{Binding TestTree}" Grid.Row="1"/>
        </Grid>

    Thank you
    Manfred


  2. Answer
    Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1183 posts

    Posted 06 Aug 2013 Link to this post

    Hello Manfred,

    You are right that an error is printed in the output console and we have already logged this behavior in our PITS. You can follow it and vote for it here.

    Please accept our apology for the inconvenience caused.

    Regards,
    Pavel R. Pavlov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 06 Aug 2013 Link to this post

    Hi Pavel,

    thank you for the answer.
    By the way - if someone else has this issue - while the problem is not solved I have a workaround.

    Change the setter to:
    <Style TargetType="telerik:RadDiagramConnection" >
        <Setter Property="Visibility" Value="{Binding Content.Visibility, RelativeSource={RelativeSource Self}}" />


    Since Content stays intact this works as expected.

    Thank you
    Manfred
Back to Top