C
C#6mo ago
Murali

MVVM Binding

I want to pass data to view model and store in list and show in collection View i am Binding in the Xaml.cs And Xmal I am getting this Issue no datacontex found for binding
No description
No description
No description
2 Replies
Mayor McCheese
Mayor McCheese6mo ago
Is that a design time error?
SpReeD
SpReeD6mo ago
Set the DataContext in XAML instead of setting it in C# in the constructor. First add the Namespace, like so
xmlns:vm="clr-namespace:MyProject.ViewModels"
xmlns:vm="clr-namespace:MyProject.ViewModels"
Then use it on the Window or Page like so
<Window.DataContext>
<vm:MyWindowViewModel/>
</Window.DataContext>
<Window.DataContext>
<vm:MyWindowViewModel/>
</Window.DataContext>
If this is plain WPF, as an alternative, you can set the DataContext for the DesignTime to get rid of the Warning. Note: It's not an Error, just the designer grumbling. To do so, add this attribute to the Window tag:
d:DataContext="{d:DesignInstance Type=local:MyView}"
d:DataContext="{d:DesignInstance Type=local:MyView}"
Note: It's actually more cleaner to set the DataContext via XAML and the latter will only set it for designtime.
Want results from more Discord servers?
Add your server