These are chat archives for AvaloniaUI/Avalonia

21st
Jan 2019
BarsikTheCaT
@BarsikTheCaT
Jan 21 09:32
Could someone explain binding syntax to me? WPF book does not cut it anymore and I can't make sense of samples on github
How does one bind a list to listbox?
Nikita Tsukanov
@kekekeks
Jan 21 09:36
Items="{Binding YourPropertyWithItemCollection}"
BarsikTheCaT
@BarsikTheCaT
Jan 21 09:44
What should be the scope of that property? Inconsistent accessibility: property type 'List<Substance>' is less accessible than property 'MainWindow.Substances' is confusing me
Nikita Tsukanov
@kekekeks
Jan 21 09:46
That's C# compiler warning
you are trying to use internal class in a public property of a public class
BarsikTheCaT
@BarsikTheCaT
Jan 21 09:48
Did I really... Omg, always forgetting modifiers for definition
thanks
BarsikTheCaT
@BarsikTheCaT
Jan 21 09:53
Well, it compiled and even launched, but didn't show anything and I am not sure if that's because the binding didn't even occur or I did not set some 'DisplayMemberPath'
ahopper
@ahopper
Jan 21 09:59
the vs output window shows some binding errors and is worth keeping an eye on
BarsikTheCaT
@BarsikTheCaT
Jan 21 10:00
It can't build with error, can it?
ahopper
@ahopper
Jan 21 10:02
bindings happen at runtime
BarsikTheCaT
@BarsikTheCaT
Jan 21 10:07
Ok, apparently I am completely retarted. What's wrong here?
<ListBox Name="SubstanceList" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Items="{Binding Substances}">
public class MainWindow : Window { public List<Substance> Substances { get; set; } = new List<Substance> { new Substance { Name = "Milk", Rho = 1011.0, C = 3.85, Lambda = 0.518, Mu = 0.000720, Pr = 5.35 }, new Substance { Name = "Cream", Rho = 974, C = 3.58, Lambda = 0.358, Mu = 0.000255, Pr = 25.5 }, new Substance { Name = "Butter", Rho = 898, C = 1.94, Lambda = 0.163, Mu = 0.0000154, Pr = 184 } };
Nikita Tsukanov
@kekekeks
Jan 21 10:11
You shouldn't be binding to the window itself
Use a separate view model class
ahopper
@ahopper
Jan 21 10:11
It would be more normal to put your list in a view model, but this will work, you need to set the datacontext of the window to itself
BarsikTheCaT
@BarsikTheCaT
Jan 21 10:12
Should I really use that much of a boilerplate with models and stuff to bind a single list?
ahopper
@ahopper
Jan 21 10:13
generally, yes :)
Nikita Tsukanov
@kekekeks
Jan 21 10:13
Applications rarely have only a single list
And when you add more stuff it gets really messy really quickly
BarsikTheCaT
@BarsikTheCaT
Jan 21 10:13
Well, mine does, at least for now, and I'd like to avoid the infamoud 'enterprise fizzbuzz'-style coding
Nikita Tsukanov
@kekekeks
Jan 21 10:13
You can set DataContext to this and it will work
If you want to go with Form1.cs coding style
In general there shouldn't be any code in your MainWindow.cs other than AvaloniaXamlLoader.Load(this)
codebehind is needed when XAML isn't enough to express your presentation logic
ahopper
@ahopper
Jan 21 10:18
there are many benefits to mvvm, an obvious one for even small projects is much code can be reused unchanged for wpf, avalonia, uwp, etc
BarsikTheCaT
@BarsikTheCaT
Jan 21 10:19
Now, MVVM might be a completely alien concept to me, but I don't think I can reference any binding source outside main window, can I?
Nikita Tsukanov
@kekekeks
Jan 21 10:20
You can also bind to static properties
ahopper
@ahopper
Jan 21 10:21
typically you set the main window DataContext to your main view model and the binding can then drill down to anything the viewmodel references
BarsikTheCaT
@BarsikTheCaT
Jan 21 10:22
Now, that's something I could get behind
ahopper
@ahopper
Jan 21 10:23
the avalonia mvvm vs template does that for you
Benedikt Schroeder
@Gillibald
Jan 21 13:17
Any thoughts on this AvaloniaUI/Avalonia#2194 ?
Nikita Tsukanov
@kekekeks
Jan 21 13:20
Templates lose some of the parent context
I believe something similar happens here:
Benedikt Schroeder
@Gillibald
Jan 21 19:04
Anyone else thinking XCodes's project format is a pain to edit?
Would love to have a Windows tool just for editing.
Anders Thorsen
@andersthorsen
Jan 21 19:11
Hi! Does anyone have a good suggestion on how to bind the Avalonia UI datagrid to a dynamic dataset? i.e. one where the columns are determined at runtime.
Jeremy Koritzinsky
@jkoritzinsky
Jan 21 19:16
I hate XCodes project format. I even prefer CMake to it.
Benedikt Schroeder
@Gillibald
Jan 21 19:20
If I wanted to do that with WPF I would implement a behavior that is doing that job for me. Thats how I introduced a Grid with data bound columns and rows. In your case that behavior would transform some model to columns and keep them in sync.
Anders Thorsen
@andersthorsen
Jan 21 19:22
hmmm. I used a Converter and Extension on the Columns property in WPF once to do something like it, but I'm not seeing how do it in Avalonia. Would the behavior way work in Avalonia or would it be a WPF only solution?
Benedikt Schroeder
@Gillibald
Jan 21 19:27
That works perfectly with Avalonia
Anders Thorsen
@andersthorsen
Jan 21 19:28
Thanks. Then I'll look into it
James Burnside
@JCBurnside
Jan 21 19:54
image.png
what does this mean?
ahopper
@ahopper
Jan 21 19:56
other than the error, does the binding do what you want?
James Burnside
@JCBurnside
Jan 21 19:57
well the xaml binds do. I am also trying to bind with code.
ahopper
@ahopper
Jan 21 20:01
I get various binding errors on datacontext change and initial binding in list view items that reference parent, the bindings ultimately work but produce these errors on the way
James Burnside
@JCBurnside
Jan 21 20:04
this is how you would bind to property changed from code right? I want a slider to have a side effect
image.png
ahopper
@ahopper
Jan 21 20:11
not sure about that code, not sure DataContext will be valid in ctor
James Burnside
@JCBurnside
Jan 21 20:12
it isn't...
ahopper
@ahopper
Jan 21 20:13
you can still attach event handler without the check
James Burnside
@JCBurnside
Jan 21 20:13
nope. DataContext is null
ahopper
@ahopper
Jan 21 20:14
yep but you can set event for change
James Burnside
@JCBurnside
Jan 21 20:14
wdym?
Benedikt Schroeder
@Gillibald
Jan 21 20:15
Why don't you do that PropertyChanged handling in your ViewModel
James Burnside
@JCBurnside
Jan 21 20:15
because I am adding stuff to a canvas.
ahopper
@ahopper
Jan 21 20:16
there is a OnDataContextChanged event or something similar
Benedikt Schroeder
@Gillibald
Jan 21 20:16
Isn't Canvas a Panel?
James Burnside
@JCBurnside
Jan 21 20:16
yes.
Benedikt Schroeder
@Gillibald
Jan 21 20:17
Just wrap your Canvas in an ItemsContol and bind to some viewmodel collection.
ahopper
@ahopper
Jan 21 20:21
@Gillibald suggestion is almost certainly a better way but it doesn't hurt to understand how to do it your way.
James Burnside
@JCBurnside
Jan 21 20:21
I can't because the calculations involve the height and width of the canvas
ahopper
@ahopper
Jan 21 20:22
they can be bound back to your viewmodel
James Burnside
@JCBurnside
Jan 21 20:23
I didn't think about that...
ahopper
@ahopper
Jan 21 20:24
and there are occasions where code behind is appropriate
The only codebehind is for keyboard handling