[MVUX] Custom Control Style not set properly when bound to the model #2372
-
Let´s create a simple Control: internal class MyControl : Control
{
public MyControl()
{
DefaultStyleKey = typeof(MyControl);
Style = StyleFactory.CreateDefaultStyle();
}
} Style for this control is created dynamically as follows: internal static class StyleFactory
{
// Here we define the default UI of the control, it can also be change by user.
public static Style<MyControl> CreateDefaultStyle() => new Style<MyControl>()
.Setters(s => s
.BorderBrush(Colors.Blue)
.BorderThickness(1)
.AutoUpdate(true)
.Template(ctrl => new StackPanel()
.BorderBrush(x => x.TemplateBind(() => ctrl.BorderBrush))
.BorderThickness(x => x.TemplateBind(() => ctrl.BorderThickness))
.Children(
new TextBlock().Text("default style"),
new TextBlock().Text(x => x.TemplateBind(() => ctrl.AutoUpdate))
)
)
);
public static Style<MyControl> CreateStyle(string txt, Color color) => new Style<MyControl>()
.Setters(s => s
.BorderBrush(color)
.BorderThickness(3)
.AutoUpdate(true)
.Template(ctrl => new StackPanel()
.BorderBrush(x => x.TemplateBind(() => ctrl.BorderBrush))
.BorderThickness(x => x.TemplateBind(() => ctrl.BorderThickness))
.Children(
new TextBlock().Text(txt),
new TextBlock().Text(x => x.TemplateBind(() => ctrl.AutoUpdate))
)
)
);
} Lest consume now the control and apply different styles to it: public sealed partial class MainPage : Page
{
public MainPage() => this
.DataContext(new BindableMainPageModel(), (page, vm) => page
.Background(ThemeResource.Get<Brush>("ApplicationPageBackgroundThemeBrush"))
.Content(
new StackPanel()
.VerticalAlignment(VerticalAlignment.Center)
.HorizontalAlignment(HorizontalAlignment.Center)
.Children(
new TextBlock()
.Text("expected style: default style -> passed"),
new MyControl()
.Margin(0, 0, 0, 10),
new TextBlock()
.Text("expected style: style instantiated in the control -> passed"),
new MyControl()
.Style(StyleFactory.CreateStyle("style instantiated in the control", Colors.DarkOrchid))
.Margin(0, 0, 0, 10),
new TextBlock()
.Text("expected style: style instantiated in the Frame -> passed"),
new MyControl()
.Margin(0, 0, 0, 10)
.Style(new Style<MyControl>()
.Setters(s => s
.BorderBrush(Colors.DarkGreen)
.BorderThickness(5)
.AutoUpdate(true)
.Template(ctrl => new StackPanel()
.BorderBrush(x => x.TemplateBind(() => ctrl.BorderBrush))
.BorderThickness(x => x.TemplateBind(() => ctrl.BorderThickness))
.Children(
new TextBlock().Text("style instantiated in the Frame"),
new TextBlock().Text(x => x.TemplateBind(() => ctrl.AutoUpdate))
)
))),
new TextBlock()
.Text("expected style: style instantiated in the model -> failed"),
new MyControl()
.Margin(0, 0, 0, 30)
.Style(() => vm.Style),
new TextBlock()
.Text("expected style: style instantiated in the model (A, B, C) -> failed"),
new ItemsControl()
.ItemsSource(() => vm.Items)
.ItemTemplate<Item>(item => new MyControl()
.Style(x => x.Binding(() => item.Style)))
)
)
);
} Model: internal partial record MainPageModel
{
public IState<Style> Style => State<Style>.Value(this, () => StyleFactory.CreateStyle("Style from model", Colors.HotPink));
public IListState<Item> Items => ListState<Item>.Value(this, () =>
[
new Item("Item A", StyleFactory.CreateStyle("Style A from model", Colors.Red)),
new Item("Item B", StyleFactory.CreateStyle("Style B from model", Colors.Green)),
new Item("Item C", StyleFactory.CreateStyle("Style C from model", Colors.Blue)),
]);
}
internal record Item(string Name, Style Style); In most of the cases, dynamically generated Tested on Windows: Note that when run on Windows with Uno version: Repro: UnoStyleAsPropertyApp.zip Note that in the example above all styles are instantiated in the control so the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This has been converted into an issue - #2400, let's continue the discussion there |
Beta Was this translation helpful? Give feedback.
This has been converted into an issue - #2400, let's continue the discussion there