Browse Source

Added IDataContextProvider.

So that non-`IStyledElement`s can take part in binding. Apply the interface to `Application`.
pull/3288/head
Steven Kirk 7 years ago
parent
commit
6a74ab560c
  1. 22
      src/Avalonia.Controls/Application.cs
  2. 13
      src/Avalonia.Styling/IDataContextProvider.cs
  3. 8
      src/Avalonia.Styling/IStyledElement.cs
  4. 2
      src/Avalonia.Styling/StyledElement.cs
  5. 4
      src/Markup/Avalonia.Markup/Data/Binding.cs

22
src/Avalonia.Controls/Application.cs

@ -32,7 +32,7 @@ namespace Avalonia
/// method.
/// - Tracks the lifetime of the application.
/// </remarks>
public class Application : AvaloniaObject, IGlobalDataTemplates, IGlobalStyles, IStyleRoot, IResourceNode
public class Application : AvaloniaObject, IDataContextProvider, IGlobalDataTemplates, IGlobalStyles, IStyleRoot, IResourceNode
{
/// <summary>
/// The application-global data templates.
@ -45,6 +45,13 @@ namespace Avalonia
private Styles _styles;
private IResourceDictionary _resources;
/// <summary>
/// Defines the <see cref="DataContext"/> property.
/// </summary>
public static readonly StyledProperty<object> DataContextProperty =
AvaloniaProperty.Register<Application, object>(
nameof(DataContext));
/// <inheritdoc/>
public event EventHandler<ResourcesChangedEventArgs> ResourcesChanged;
@ -56,6 +63,19 @@ namespace Avalonia
Name = "Avalonia Application";
}
/// <summary>
/// Gets or sets the Applications's data context.
/// </summary>
/// <remarks>
/// The data context property specifies the default object that will
/// be used for data binding.
/// </remarks>
public object DataContext
{
get { return GetValue(DataContextProperty); }
set { SetValue(DataContextProperty, value); }
}
/// <summary>
/// Gets the current instance of the <see cref="Application"/> class.
/// </summary>

13
src/Avalonia.Styling/IDataContextProvider.cs

@ -0,0 +1,13 @@
namespace Avalonia
{
/// <summary>
/// Defines an element with a data context that can be used for binding.
/// </summary>
public interface IDataContextProvider : IAvaloniaObject
{
/// <summary>
/// Gets or sets the element's data context.
/// </summary>
object DataContext { get; set; }
}
}

8
src/Avalonia.Styling/IStyledElement.cs

@ -10,7 +10,8 @@ namespace Avalonia
IStyleHost,
ILogical,
IResourceProvider,
IResourceNode
IResourceNode,
IDataContextProvider
{
/// <summary>
/// Occurs when the control has finished initialization.
@ -27,11 +28,6 @@ namespace Avalonia
/// </summary>
new Classes Classes { get; set; }
/// <summary>
/// Gets or sets the control's data context.
/// </summary>
object DataContext { get; set; }
/// <summary>
/// Gets the control's logical parent.
/// </summary>

2
src/Avalonia.Styling/StyledElement.cs

@ -24,7 +24,7 @@ namespace Avalonia
/// - Implements <see cref="ILogical"/> to form part of a logical tree.
/// - A collection of class strings for custom styling.
/// </summary>
public class StyledElement : Animatable, IStyledElement, ISetLogicalParent, ISetInheritanceParent
public class StyledElement : Animatable, IDataContextProvider, IStyledElement, ISetLogicalParent, ISetInheritanceParent
{
/// <summary>
/// Defines the <see cref="DataContext"/> property.

4
src/Markup/Avalonia.Markup/Data/Binding.cs

@ -231,9 +231,9 @@ namespace Avalonia.Data
{
Contract.Requires<ArgumentNullException>(target != null);
if (!(target is IStyledElement))
if (!(target is IDataContextProvider))
{
target = anchor as IStyledElement;
target = anchor as IDataContextProvider;
if (target == null)
{

Loading…
Cancel
Save