Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ItemControl not binding #13421

Closed
mdblack98 opened this issue Oct 28, 2023 · 12 comments
Closed

ItemControl not binding #13421

mdblack98 opened this issue Oct 28, 2023 · 12 comments
Labels

Comments

@mdblack98
Copy link

Describe the bug

ItemControl in XAML MVVM not binding -- could be my error but I can't see it.

To Reproduce

Using Avalonia 11.0.5 and Visual Studio' 2022 Preview 17.8.0 Preview 5.0 on Windows 11

Use this zip'd project file and build it. Also the XAML does not display.
https://www.dropbox.com/scl/fi/pj1xopwgujxzquqd2ce71/AvaloniaNumberApp.zip?rlkey=5aeqh6lxd4sj4mouxlndkm5qb&dl=0
Error I'm seeing is:
Severity Code Description Project File Line Suppression State Details
Error AVLN:0004 Unable to find a setter that allows multiple assignments to the property Content of type Avalonia.Controls:Avalonia.Controls.ContentControl Line 1, position 2. AvaloniaNumberApp C:\Users\mdbla\source\repos\AvaloniaNumberApp\Views\MainWindow.axaml 1

Expected behavior

Successful compile and visual display of XAML

Screenshots

Screenshots N/A

Additional context

N/A

AvaloniaNumberApp.zip

@mdblack98 mdblack98 added the bug label Oct 28, 2023
@thevortexcloud
Copy link
Contributor

thevortexcloud commented Oct 29, 2023

Your XAML does not look valid. You for some reason have a nested window declaration?

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:AvaloniaNumberApp.ViewModels"
        x:Class="AvaloniaNumberApp.Views.MainWindow"
        Title="Avalonia Number App">
	<Window>
		<local:MainWindowViewModel/>
	</Window>

I assume this is actually meant to be

	<Design.DataContext>
		<local:MainWindowViewModel/>
	</Design.DataContext>

https://docs.avaloniaui.net/docs/next/guides/implementation-guides/how-to-use-design-time-data

@mdblack98
Copy link
Author

Thanks for seeingthat. Having fixed that (openai.com is helping write this thing) I am still getting the binding error....this is reminding me of C++ hell where you can't see what it's trying to do. It's saying it can't find "" What it doesn't say is what exactly it is looking for. (e.g. Can't find Digits.set() or Digits.set(value) type error.
But I've got this namespace reference:
clr-namespace:AvaloniaNumberApp.ViewModels
And in MainWIndowViewModels.cs it has what seems to be the correct namespace reference and set/get for Digits
namespace AvaloniaNumberApp.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
public ObservableCollection Digits { get; set; }
Severity Code Description Project File Line Suppression State Details
Error AVLN:0004 Unable to find suitable setter or adder for property Items of type Avalonia.Controls:Avalonia.Controls.ItemsControl for argument Avalonia.Markup.Xaml:Avalonia.Markup.Xaml.MarkupExtensions.CompiledBindingExtension, available setter parameter lists are:
System.Object
System.Object Line 10, position 17. AvaloniaNumberApp C:\Users\mdbla\source\repos\AvaloniaNumberApp\Views\MainWindow.axaml 10

@rabbitism
Copy link
Contributor

Several issues to fix and make the app start to run.

  1. As described in error message, "Unable to find suitable setter or adder for property Items of type", so you need to bind to ItemsSource instead of Items.
<ItemsControl ItemsSource="{Binding Digits}">
  1. Avalonia by default enabled compile binding, which enables strong typed binding in xaml.
    So you need to set the x:DataType="local:MainWindowViewModel"
    image

Also you are trying to bind to DataContext of parent control, which also requires a type casting. like below.

<Button 
    Command="{Binding ((local:MainWindowViewModel)DataContext).IncreaseCommand, RelativeSource={RelativeSource AncestorType=Window}}"
    CommandParameter="{Binding}"
    Content="^"/>

@timunie
Copy link
Contributor

timunie commented Oct 29, 2023

You cannot blindly trust a chat bot, sorry to say.

Also this is not an avalonian bug. So please open a Q&A instead of a bug report. Or join us on Telegram.

Thx.

@timunie timunie added question and removed bug labels Oct 29, 2023
@mdblack98
Copy link
Author

Well the chatbot knows a lot more about it than I do and I'm trying to teach it too.
I've found the tutorials to be useless.
So I really appreciate the help here.
Now with the MainWindow.axaml with those changes I'm getting this error -- which I maintain is not specific enough. It's not line#1 and not position #2 either. When I remove the x.DataType I get an appropriate error like this which is more useful
Error AVLN:0004 Cannot parse a compiled binding without an explicit x:DataType directive to give a starting data type for bindings. Line 10, position 17. AvaloniaNumberApp C:\Users\mdbla\source\repos\AvaloniaNumberApp\Views\MainWindow.axaml 10

And once I put in the xDataType I get this incorrect line# and position#
Error AXN0002 Unable to resolve type x from namespace https://github.com/avaloniaui Line 1, position 2. AvaloniaNumberApp C:\Users\mdbla\source\repos\AvaloniaNumberApp\CSC 1


<Window.DataTemplates>
local:MainWindowViewModel/
</Window.DataTemplates>


<ItemsControl.ItemTemplate>







</ItemsControl.ItemTemplate>


@timunie
Copy link
Contributor

timunie commented Oct 29, 2023

Please start here https://github.com/AvaloniaUI/Avalonia.Samples

Your project seems to be a kinda broken atm. We cannot fix all of those issues for you. You need to learn the fundamentals of Avalonia and MVVM first. It's really better if you know what you or the bot is doing. Once you are used to it, you can make start hacking.

@mdblack98
Copy link
Author

FYI...If I add this it now compiles and displays correctly. Not desirable but I can't figure out how to get it compiled without it.
x:CompileBindings="false"

@timunie
Copy link
Contributor

timunie commented Oct 29, 2023

Make sure to read docs about compiled bindings please.

@thevortexcloud
Copy link
Contributor

thevortexcloud commented Oct 29, 2023

Well the chatbot knows a lot more about it than I do and I'm trying to teach it too.
I've found the tutorials to be useless.

The chatbot as you are seeing is likely based on old examples, many of which probably wont work anymore. Avalonia has had a lot of subtle breaking changes since v11.0 was released. So while it may "know" more than you, what it knows probably does not work anymore, and I also would be surprised if it ends up getting mixed up with WPF and other XAML code. Also using an AI chatbot for this sort of things is also not a good idea. As you have also seen it can generate completely invalid code. Which you may not catch if you don't not know what you are doing. It's frankly a tool that is only useful if you know what it should be doing.

As for the tutorials. I agree that some of them are very complicated and skip over some information, but can you please explain what exactly you problem you found so someone can fix it? They do work for learning how to use Avalonia. I personally used them myself when I started.

Also in many cases you can just read WPF's documentation and examples. About ~60% of it applies to Avalonia due to how similar they are.

@mdblack98
Copy link
Author

mdblack98 commented Oct 29, 2023 via email

@rabbitism
Copy link
Contributor

That's exactly what I said, if you need compile binding, you need to set the x:DataType as in my screenshot, and add type casting in button binding.

@timunie
Copy link
Contributor

timunie commented Oct 30, 2023

@mdblack98 I'll make a Q&A from this thread, as it is not an Avalonia bug. You are still welcome to ask if you need more help. For quick questions try also our chat https://t.me/Avalonia

@AvaloniaUI AvaloniaUI locked and limited conversation to collaborators Oct 30, 2023
@timunie timunie converted this issue into discussion #13430 Oct 30, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

4 participants