Having trouble with a custom UserControl and it's properties #18064
Replies: 3 comments 1 reply
-
The same confusion here. I can't find any documentation on how to access properties in XML. Can they not be accessed directly? Is it required to use them with a view model? |
Beta Was this translation helpful? Give feedback.
-
The Simplifying the code a bit (keep the styled properties as-is) and demonstrating both approaches: <UserControl
x:Class="AvaloniaApplication43.TestControl"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:v="using:AvaloniaApplication43"
x:Name="root"
mc:Ignorable="d">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding #root.Caption}" />
<TextBlock x:Name="TheTextBlock" Text="{Binding $parent[v:TestControl].Text}" />
</StackPanel>
</UserControl> public partial class TestControl : UserControl
{
public TestControl()
{
InitializeComponent();
}
// Just to prove it works
protected override async void OnLoaded(RoutedEventArgs e)
{
base.OnLoaded(e);
await Task.Delay(3000);
Text = Path.GetRandomFileName();
Caption = Path.GetRandomFileName();
}
// Properties...
} I exclusively reserve the |
Beta Was this translation helpful? Give feedback.
-
If you need a custom control, I highly suggest to inherit TemplatedControl or ContentControl and use TemplateBindings. See Avalonia.Samples > RatingControl for reference. |
Beta Was this translation helpful? Give feedback.
-
Hello there, I've defined a UserControl as follows:
And it's code-behind:
However when I use it as such:
<local:CopyableToClipboardTextBoxUserControl Caption="Id" Text="Id" />
There is no text displayed, can someone help me get it working please?
Beta Was this translation helpful? Give feedback.
All reactions