Browse Source

Make ContentPresenter update its DataContext.

Previous method introduced in 4241fd8331
caused issue #503, because ContentPresenter should only use Content as
its DataContext when it is not a control. Take the WPF approach and make
ContentPresenter choose its own DataContext. Fixes #503.
pull/473/merge
Steven Kirk 11 years ago
parent
commit
4e63eb8f07
  1. 12
      src/Perspex.Controls/Presenters/ContentPresenter.cs
  2. 22
      tests/Perspex.Controls.UnitTests/Presenters/ContentPresenterTests.cs

12
src/Perspex.Controls/Presenters/ContentPresenter.cs

@ -2,6 +2,7 @@
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Reactive.Linq;
using Perspex.Controls.Primitives;
using Perspex.Controls.Templates;
using Perspex.Layout;
@ -83,6 +84,17 @@ namespace Perspex.Controls.Presenters
TemplatedParentProperty.Changed.AddClassHandler<ContentPresenter>(x => x.TemplatedParentChanged);
}
/// <summary>
/// Initializes a new instance of the <see cref="ContentPresenter"/> class.
/// </summary>
public ContentPresenter()
{
var dataContext = this.GetObservable(ContentProperty)
.Select(x => x is IControl ? PerspexProperty.UnsetValue : x);
Bind(Control.DataContextProperty, dataContext);
}
/// <summary>
/// Gets or sets a brush with which to paint the background.
/// </summary>

22
tests/Perspex.Controls.UnitTests/Presenters/ContentPresenterTests.cs

@ -130,6 +130,28 @@ namespace Perspex.Controls.UnitTests.Presenters
Assert.IsType<Decorator>(target.Child);
}
[Fact]
public void Assigning_Control_To_Content_Should_Not_Set_DataContext()
{
var target = new ContentPresenter
{
Content = new Border(),
};
Assert.False(target.IsSet(Control.DataContextProperty));
}
[Fact]
public void Assigning_NonControl_To_Content_Should_Set_DataContext()
{
var target = new ContentPresenter
{
Content = "foo",
};
Assert.Equal("foo", target.DataContext);
}
private class TestContentControl : TemplatedControl
{
public IControl Child { get; set; }

Loading…
Cancel
Save