I'm new both to WinUI and XAML and I'm creating a WinUI 3 Library that contains various CustomControl.
They're all independent and can be used stand alone. But one of those controls, is made by embedding some other custom controls that are in the library.
XAML of Container control
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyCustomControls">
<Style TargetType="local:CustomContainer">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomContainer">
<StackPanel>
<local:CustomTextBlock x:Name="Text"
DisplayMode="{TemplateBinding DisplayMode}"
Message="{TemplateBinding Message}">
</local:CustomTextBlock>
<local:CustomIndicator x:Name="Indicator"></local:CustomIndicator>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
As you can see in the sample, the CustomTextBlock custom control contains 2 DependecyProperty (DisplayMode and Message) that I need to "replicate" on the CustomContainer control to be able to set them when I want to use the CustomContainer on a page.
Here in the XAML I've used TemplateBinding with 2 DependecyProperty that I should declare on the code behind of CustomContainer.
Code behind of CustomTextBlock control
private static readonly DependencyProperty DisplayModeProperty = DependencyProperty.Register(
nameof(DisplayMode), typeof(bool), typeof(CustomTextBlock), new PropertyMetadata(null));
public bool DisplayMode
{
get => (bool)GetValue(DisplayModeProperty);
set => SetValue(DisplayModeProperty, value);
}
private static readonly DependencyProperty MessageProperty = DependencyProperty.Register(
nameof(Message), typeof(string), typeof(CustomTextBlock), new PropertyMetadata(default(string), (d, e) => ((CustomTextBlock)d).MessageChanged(d, e)));
public string Message
{
get => (string)GetValue(MessageProperty);
set => SetValue(MessageProperty, value);
}
How can I expose the 2 properties of CustomTextBlock on the CustomContainer control so that those values directly sets the underlying properties? Do they still need to be DependencyProperty type?
It seems something like wrapping or inheritance concept, but I'm not able to figure it out, especially for the Message property that is also registered with an event handler.