[WPF]How to binding in the UserControl.xaml at the same time load UserControl.xaml UI?
I'm using the wpf-ui template to use the wpf mvm pattern. I focused on 4 files
I want to use DashboardPage.xaml, DashboardPageViewModel.cs , User.xaml, UserViewModel.cs . I want to get the ui of the UserControl file User.xaml from the Page file DashboardPage.xaml.
By the way, I implemented a code that allocates datacontext to the behind code to implement a button-like control function in User.xaml. Of course, I implemented the same code that allocates DataContext to the DashboardPage.xaml.cs behind code. The problem is that the code implemented by User.xaml.cs causes the "Object reference not set to an instance of an object." error.
I wonder why the problem occurred.
*picture 1 is error cature.
**picture2 is that if annotate ViewModel assign part and DataContext assign part in the User.xaml.cs , then no happened error.
but this is not what i want. I wanna implement control at the UserControl.xaml ( User.xaml)
How to solve this problem. and Why did the error occur?
below is code.
https://github.com/Jlim01/UiDesktopApp1.git
GitHub
GitHub - Jlim01/UiDesktopApp1
Contribute to Jlim01/UiDesktopApp1 development by creating an account on GitHub.
21 Replies
its complaining your User.xaml.cs does not have an accessible constructor
this code is wrong code?
yes because the way your useing the usercontrol it cannot be initialized
so instead u sets the datacontext via xaml
Actually Im following "WPF-UI" Template . so I thought UserControl also is the same code with Page code
if u wanted to use it like that u would need to use a ContentControl to load the control by its vm
you're not using it in the same way
the page code is being used in a presenter
the usercontrol ur loading directly
yea
actually I don't know how to use that..
presenter you are saying is this part?
<ContentPresenter x:Name="RootContentDialog" Grid.Row="0" />
thanks your kind reply ! , but Im wandering how to know that ContentPresenter is related to Page files like DashboardView.xaml
It's so difficult for me
<ContentPresenter x:Name="RootContentDialog" Grid.Row="0" /> because I thought This code is not anything
I just delete
<ContentPresenter x:Name="RootContentDialog" Grid.Row="0" /> this code , and then executed program. the result is no problem...
my bad yeah that does nothing the navigationview
the RootNativation resolves what page to load
which is handled by your navigation service
but for the usercontrol
your directly inserting it to xaml
<user:User Grid.Row="1"/>
so it does not understand how initialize
so u would either use a ContentControl
and resolve it like u do for pages
or u simple have the datacontext applied to xaml
since it will just instantiate it for u when constructed
it can use it
is there any tutorial or sample code? resolving using ContentControl.
I dont know of any no sorry.
nono, That's already enough answer. thanks!
Use the /close command to mark a forum thread as answered
I send to you message,
send it here, I dont take dms
I want to send a coffee coupon to you. What should I do?
its ok we don't do this for compensation
its also against the rules to offer compensation
everyone is here to learn and to help.
thank you, If I become an expert, I will be of help to others like you.
u dont need to be an expert to help 😉 I am definitively not one either