-
Let´s create a simple templated control, that exposes one single public class MyControl : Control
{
public MyControl() {}
public bool AutoUpdate
{
get { return (bool)GetValue(AutoUpdateProperty); }
set { SetValue(AutoUpdateProperty, value); }
}
public static readonly DependencyProperty AutoUpdateProperty =
DependencyProperty.Register(
nameof(AutoUpdate),
typeof(bool),
typeof(MyControl),
new PropertyMetadata(
false,
(s, e) => ((MyControl)s)?.OnAutoUpdateChanged(e))
);
private void OnAutoUpdateChanged(DependencyPropertyChangedEventArgs e)
{
Debug.WriteLine($"MyProperty Changed from [{e.OldValue}] to [{e.NewValue}]");
}
} Let´s bind this property to the MainPageModel state public sealed partial class MainPage : Page
{
public MainPage() => this
.DataContext(new BindableMainPageModel(), (ctrl, vm) => this
.Background(ThemeResource.Get<Brush>("ApplicationPageBackgroundThemeBrush"))
.Content(new StackPanel()
.VerticalAlignment(VerticalAlignment.Center)
.HorizontalAlignment(HorizontalAlignment.Center)
.Children(
new CheckBox()
.Content("MyOption")
.IsChecked(x => x.Binding(() => vm.MyOption).TwoWay()),
// binding below triggers correctly, but
// stops to trigger when state management added to MyControl
new MyControl()
.AutoUpdate(x => x.Binding(() => vm.MyOption).TwoWay())
)));
}
internal partial record MainPageModel
{
public IState<bool> MyOption => State<bool>.Value(this, () => true);
} I expect that every time when CheckBox is clicked, the AutoUpdate But now, do modify the template control by adding a state management to it: public MyControl()
=> this.DataContext(new BindableMyModel(), (page, vm) => { }); where MyModel is: This brakes the binding to custom property and Tested on
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 12 replies
-
Any news on this one? |
Beta Was this translation helpful? Give feedback.
-
@dansiegel I promised you more complex demo, here you have one: UnoAbcApp.zip The attached UnoAbcApp does the following:
Expected behavior: Each time FeatureA, FeatureB, FeatureC General question is:
|
Beta Was this translation helpful? Give feedback.
This comment has been hidden.
This comment has been hidden.
-
Hey @kucint, about the crash at startup when adding |
Beta Was this translation helpful? Give feedback.
Hey @kucint, I've made a sample with both a
UserControl
and aControl
that should give you some insight on how to fix your issue:UnoAppTemplateBindingWorking.zip
The main point is really to note that a "binding" is relative to a "source". When not specified, the default/implicit source of that binding is the inherited
DataContext
, and all bindings on an element are relative to theDataContext
of that given element.This means that is you have:
the binding on
MyProperty
is relative to theDataContext
ofMYControl
, so it's equivalent to: