Example #1
0
        public void DataContextTest()
        {
            ContentPresenter c = (ContentPresenter)XamlReader.Load(@"
<ContentPresenter xmlns=""http://schemas.microsoft.com/client/2007"">
	<ContentPresenter.ContentTemplate>
		<DataTemplate>
			<Rectangle DataContext=""{Binding DataContext}"" />
		</DataTemplate>
	</ContentPresenter.ContentTemplate>
	<Rectangle />
</ContentPresenter>");

            Assert.IsNull(c.DataContext);
            Assert.AreEqual(DependencyProperty.UnsetValue, c.ReadLocalValue(ContentPresenter.DataContextProperty), "unset 1");
            c.Content = new Rectangle();
            Assert.IsNull(c.DataContext);
            Assert.AreEqual(DependencyProperty.UnsetValue, c.ReadLocalValue(ContentPresenter.DataContextProperty), "unset 2");

            CreateAsyncTest(c,
                            () => {
                Assert.IsNull(c.DataContext);
                Assert.AreEqual(DependencyProperty.UnsetValue, c.ReadLocalValue(ContentPresenter.DataContextProperty), "unset 3");
            },
                            () => c.Content = new Rectangle(),
                            () => {
                Assert.AreEqual(DependencyProperty.UnsetValue, c.ReadLocalValue(ContentPresenter.DataContextProperty), "unset 4");
                Assert.IsNull(c.DataContext);
            }
                            );
        }
Example #2
0
        public void ChangeDefaultTemplate()
        {
            ContentControl c = (ContentControl)XamlReader.Load(@"
<ContentControl xmlns=""http://schemas.microsoft.com/client/2007"">
    <ContentControl.Template>
        <ControlTemplate>
            <ContentPresenter />
        </ControlTemplate>
    </ContentControl.Template>
</ContentControl>");

            c.Content         = new ConcreteFrameworkElement();
            c.ContentTemplate = new DataTemplate();
            ContentPresenter p = null;

            CreateAsyncTest(c,
                            () => Assert.VisualChildren(c, "#1",
                                                        new VisualNode <ContentPresenter> ("#2", (pr) => p = pr)
                                                        ),
                            () => {
                Assert.IsNull(p.DataContext, "#3");
                Assert.AreEqual(c.Content, p.Content, "#4");
            },
                            () => {
                Assert.AreSame(c.Content, p.Content);
                Assert.IsInstanceOfType <TemplateBindingExpression> (p.ReadLocalValue(ContentPresenter.ContentProperty), "#5");

                Assert.AreSame(c.ContentTemplate, p.ContentTemplate);
                Assert.IsInstanceOfType <TemplateBindingExpression> (p.ReadLocalValue(ContentPresenter.ContentTemplateProperty), "#6");
            },
                            () => {
                Assert.VisualChildren(p, "#7");
            }
                            );
        }
Example #3
0
        public void DataTemplateTest4()
        {
            ContentControl c = (ContentControl)XamlReader.Load(@"
<ContentControl xmlns=""http://schemas.microsoft.com/client/2007"">
    <ContentControl.ContentTemplate>
        <DataTemplate>
            <Grid>
				<ContentPresenter />
			</Grid>
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>");

            c.Content = new ConcreteFrameworkElement();
            ContentPresenter p = null;

            CreateAsyncTest(c, () =>
                            Assert.VisualChildren(c, "#1",
                                                  new VisualNode <ContentPresenter> ("#a",
                                                                                     new VisualNode <Grid> ("#b",
                                                                                                            new VisualNode <ContentPresenter> ("#c", pr => p = pr)
                                                                                                            )
                                                                                     )
                                                  ),
                            () => {
                Assert.AreEqual(DependencyProperty.UnsetValue, p.ReadLocalValue(ContentPresenter.ContentProperty), "#2");
                Assert.AreEqual(DependencyProperty.UnsetValue, p.ReadLocalValue(ContentPresenter.ContentTemplateProperty), "#3");
            }
                            );
        }
Example #4
0
        public void DataContextTest7()
        {
            // A null content is copied to the datacontext too.
            ContentPresenter presenter = new ContentPresenter();

            CreateAsyncTest(presenter,
                            () => {
                presenter.Content = new object();
            }, () => {
                Assert.AreEqual(presenter.Content, presenter.ReadLocalValue(ContentPresenter.DataContextProperty), "#1");
                presenter.Content = null;
            }, () => {
                Assert.AreEqual(null, presenter.ReadLocalValue(ContentPresenter.DataContextProperty), "#2");
            }
                            );
        }
Example #5
0
        public void MultiplePresenters()
        {
            ControlTemplate template = CreateTemplate(@"
<ContentControl>
	<Grid>
		<ContentPresenter/>
		<ContentPresenter/>
		<ContentPresenter/>
	</Grid>
</ContentControl>
");
            ContentControl  c        = new ContentControl {
                Template = template,
                Content  = "content"
            };

            CreateAsyncTest(c, () => {
                Grid grid = null;
                Assert.VisualChildren(c,
                                      new VisualNode <ContentControl> ("#1",
                                                                       new VisualNode <ContentPresenter> ("#2",
                                                                                                          new VisualNode <Grid> ("#3", g => grid = g, null)
                                                                                                          )
                                                                       )
                                      );
                for (int i = 0; i < 3; i++)
                {
                    ContentPresenter p = (ContentPresenter)grid.Children [i];
                    Assert.IsInstanceOfType <TemplateBindingExpression> (p.ReadLocalValue(ContentPresenter.ContentProperty), "#4." + i);
                    Assert.AreEqual("content", p.Content, "#5." + i);
                }
            });
        }
Example #6
0
        public void DataContextTest_DataContextUpdatesAsync()
        {
            var presenter = new ContentPresenter();

            presenter.SetBinding(ContentPresenter.DataContextProperty, new Binding());
            Assert.IsInstanceOfType <BindingExpression> (presenter.ReadLocalValue(ContentPresenter.DataContextProperty), "#1");

            presenter.Content = new object();
            Assert.IsInstanceOfType <BindingExpression> (presenter.ReadLocalValue(ContentPresenter.DataContextProperty), "#2");

            TestPanel.Children.Add(presenter);
            Assert.IsInstanceOfType <BindingExpression> (presenter.ReadLocalValue(ContentPresenter.DataContextProperty), "#3");

            Enqueue(() => {
                Assert.AreSame(presenter.Content, presenter.ReadLocalValue(ContentPresenter.DataContextProperty), "#4");
            });
            EnqueueTestComplete();
        }
Example #7
0
        public void DataContextTest2()
        {
            // Changing the content updates the datacontext
            ContentPresenter c = new ContentPresenter();

            c.Content = new object();
            CreateAsyncTest(c,
                            () => {
                Assert.AreEqual(c.Content, c.DataContext, "#1");
                Assert.AreEqual(c.Content, c.ReadLocalValue(ContentPresenter.DataContextProperty), "#1b");
                c.Content = new object();
            },
                            () => {
                Assert.AreEqual(c.Content, c.DataContext, "#2");
                Assert.AreEqual(c.Content, c.ReadLocalValue(ContentPresenter.DataContextProperty), "#2b");
            }
                            );
        }
Example #8
0
        public void DataContextTest5()
        {
            // When the ContentPresenter is in the tree, its DataContext gets set to ContentPresenter.Content
            // when it is loaded
            object           o = new object();
            ContentPresenter c = new ContentPresenter {
                DataContext = o
            };

            Assert.AreEqual(o, c.DataContext, "#1");
            CreateAsyncTest(c,
                            () => {
                Assert.IsNull(c.DataContext, "#2");
                Assert.IsNull(c.ReadLocalValue(ContentPresenter.DataContextProperty), "#2b");
            },
                            () => {
                c.DataContext = o;
                Assert.AreEqual(o, c.DataContext, "#3");
                Assert.AreEqual(o, c.ReadLocalValue(ContentPresenter.DataContextProperty), "#3b");
            },
                            () => Assert.AreEqual(o, c.DataContext, "#4")
                            );
        }
Example #9
0
        public void DataContextTest5b()
        {
            // When the ContentPresenter is in the tree, its DataContext gets set to ContentPresenter.Content
            // when it is loaded
            object           content = new object();
            ContentPresenter c       = new ContentPresenter {
                Content = content
            };

            CreateAsyncTest(c,
                            () => {
                Assert.AreEqual(content, c.DataContext, "#1");
                Assert.AreEqual(content, c.ReadLocalValue(FrameworkElement.DataContextProperty), "#2");
            }
                            );
        }