AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.91k stars 2.24k forks source link

Unable to find suitable setter or adder for property when using binding for usercontrols #3682

Closed cemeceme closed 4 months ago

cemeceme commented 4 years ago

I'm trying to create a usercontrol that will accept some properties, however it appears that it fails with the error:

Error XAMLIL: Unable to find suitable setter or adder for property stringProperty of type Client:Client.test for argument Avalonia.Markup:Avalonia.Data.Binding, available setter parameter lists are:
System.String

The main window xaml is:

<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:local="clr-namespace:Client"
        x:Class="Client.MainWindow"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="600"
        Title="Title" Background="LightGray" Width="800" Height="600">
    <TextBlock Text="{Binding dataString}"/>
    <local:test stringProperty="{Binding dataString}"/>
</Window>

MainWindows ViewModel is:

namespace Client
{
    public class MainWindowViewModel
    {
        public string dataString{ get{ return("sample text");} }
    }
}

The test xaml is:

<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"
             x:Class="Client.test" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450">
    <TextBlock Text="Some text"/>
</UserControl>

And the code-behind for test is:

namespace Client
{
    public class test : UserControl
    {
        public string stringProperty { get; set; }

        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
        }

        public test()
        {
            this.InitializeComponent();
        }
    }
}

I have also tried to use direct/attached/styled/etc. properties, but nothing seems to have helped.

One thing I have noticed however, is that when not using databinding in the main window, the code does compile and work as expected. I left out bindings in the usercontrol and related code to make sure that wasn't causing the issue, and when using just a static string the property is indeed set properly.

I'm fairly new to Avalonia and mvvm so I might have misconceptions about how usercontrols and their properties work. So if this is intended behavior and I'm using the wrong control or something do tell me, as I have been stuck for the past few days.

Gillibald commented 4 years ago

http://avaloniaui.net/docs/authoring-controls/defining-properties

cemeceme commented 4 years ago

Thanks for the help! I came across that piece of documentation multiple times, but I think I just figured out why I couldn't get it to work before.

For some reason if I register the property using:

public static readonly AvaloniaProperty<string> stringPropertyProperty =
    AvaloniaProperty.Register<test, string>("stringProperty");

it works, but if I change the name of the variable to something like:

public static readonly AvaloniaProperty<string> testProperty =
    AvaloniaProperty.Register<test, string>("stringProperty");

which I was using while testing this, then I get the error as before. Trying some more names, it seems any variation from stringPropertyProperty causes an error on my end.

Is there some sort of required naming scheme for defining properties?

Gillibald commented 4 years ago

You need to fulfill the naming convention.

string String { get.... ; set....} AvaloniaProperty StringProperty

MarchingCube commented 4 years ago

@grokys I guess another part that needs to be covered in updated documentation for properties.

derekantrican commented 4 years ago

Oh man, @Gillibald - that caused me so much headache! Definitely echo @MarchingCube that somewhere this needs to say in big red text and neon-flashing lights that the naming really matters in this case