Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React-like contexts #3130

Closed
handicraftsman opened this issue Oct 17, 2019 · 3 comments
Closed

React-like contexts #3130

handicraftsman opened this issue Oct 17, 2019 · 3 comments

Comments

@handicraftsman
Copy link

handicraftsman commented Oct 17, 2019

In React Context is an object which contains some value and exports a Provider - an element which provides value to all children, overridding parents' values, and "users" - functions which get value of the nearest provider of given type. On context update users are updated too.

Would be nice in conjunction with control themes as in #2769 .

This might look like below; sorry if i have any mistakes - i am not good enough in xaml, although i hope you get the idea.

class ThemeContext : Context {
  public Color PrimaryColor { get; set; }
}
<ThemeContext PrimaryColor="#123456">
  <ContextUser Context={x:Type} x:Name="theme">
    <SomeElement Color={x:Bind theme.PrimaryColor} />
  </ContextUser>
</ThemeContext>
@kekekeks
Copy link
Member

kekekeks commented Oct 17, 2019

How exactly is it different from DynamicResource?

<ContentControl>
     <ContentControl.Resources>
           <Color x:Key="ThemePrimaryColor">#123456</Color>
     </ContentControl.Resources>
     <SomeElement Color="{DynamicResource ThemePrimaryColor}"/>
</ContentControl>

Also, you can achieve contexts via inherited attached properties.

e. g.

class ThemeContext
{
      public Color PrimaryColor { get; set; }
      public static AvaloniaProperty CurrentProperty = AvaloniaProperty.RegisterAttached(...
      ... attached getter and setter here ...
}
<ContentControl>
     <local:ThemeContext.Current>
          <local:ThemeContext Color="#123456"/>
     </local:ThemeContext.Current>
     <SomeElement Color="{Binding (local:ThemeContext.Current).Color, RelativeSource={RelativeSource Self}}"/>
</ContentControl>

@kekekeks
Copy link
Member

Another way would be a class inheriting from ContentControl, e. g.

class ThemeContext : ContentControl
{
      public Color PrimaryColor { get; set; }
}
<local:ThemeContext PrimaryColor="#123456">

    <SomeElement Color="{Binding $parent[local:ThemeContext].PrimaryColor}" />

</ThemeContext>

@handicraftsman
Copy link
Author

Hmm, nice. Sorry for asking without knowing that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants