Avalonia Binding Custom Control throws System.InvalidCastException

23 Views Asked by At

I have a Avalonia project in C# that was just setup. Currently I am testing if Avalonia fits my requirements. Currently I struggle at a simple point. I want to create a custom user control of type UserControl and provide a property in that control, that should be set by a binding from the view that is using the control.

What I have:

  1. A new MainWindow.axaml with this content
<Window 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:c="using:My.Avalonia.Controls"
        xmlns:vm="using:My.Avalonia.ViewModels"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="My.Avalonia.Views.MainWindow"
        Icon="/Assets/avalonia-logo.ico"
        Title="My.Avalonia"
        x:DataType="vm:MainViewModel"
        >
<StackPanel>
  <c:MyControl Text="{Binding MyObject.DisplayName, Mode=OneWay}"/>
  <!-- This works: -->
  <Label Content="{Binding MyObject.DisplayName, Mode=OneWay}"/>
</StackPanel>
  1. A new UserControl, created by the Avalonia Template. In MyControl.axaml:
<UserControl 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:c="using:My.Avalonia.Controls"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="My.Avalonia.Controls.MyControl"
             x:Name="iconControl"
             x:DataType="c:MyControl"
             >
  <Design.DataContext>
    <c:MyControl />
  </Design.DataContext>

  <Label Content="{Binding Text, Mode=OneWay}" Background="Aqua"/>
</UserControl>

In the MyControl.axaml.cs:

using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using My.Avalonia.Models;
using System.Collections.Specialized;
using System.ComponentModel;

namespace My.Avalonia.Controls
{
    public partial class MyControl : UserControl, INotifyPropertyChanged
    {

        public MyControl()
        {
            InitializeComponent();
            DataContext = this;
        }

        public static readonly StyledProperty<string?> TextProperty = AvaloniaProperty.Register<MyControl, string?>(nameof(Text));

        public string? Text
        {
            get { return GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }
    }
}

My Problem: When I run the program, the binding simply not work. The source string variable has a valid string as when I bind it to a standard label, it works. In the Debug output I see the following line: Exception thrown: 'System.InvalidCastException' in System.Private.CoreLib.dll. It disappears, if I remove the binding. It stays if I remove the Label in the MyControl, so the source seems to be the binding between the MainWindow and the UserControl. I don't understand why this is thrown as both have the type string?. Did I miss something in the Avalonia documentation?

0

There are 0 best solutions below