C
C#•8mo ago
JJ

[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.
No description
No description
21 Replies
JJ
JJOP•8mo ago
If you can't enter github, then You can download this text file and see six code included View.xaml/Viewxaml.cs / ViewModel.cs
leowest
leowest•8mo ago
its complaining your User.xaml.cs does not have an accessible constructor
public partial class User : UserControl
{
public User()
{
InitializeComponent();
}
}
public partial class User : UserControl
{
public User()
{
InitializeComponent();
}
}
<UserControl x:Class="UiDesktopApp1.Views.User.User"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:UiDesktopApp1.Views.User"
xmlns:vm="clr-namespace:UiDesktopApp1.ViewModels.User"
mc:Ignorable="d"
Background="White"
d:DataContext="{d:DesignInstance local:User}"
d:DesignHeight="120" d:DesignWidth="300">
<UserControl.DataContext>
<vm:UserViewModel/>
</UserControl.DataContext>
<Grid>
<StackPanel>
<Button Content="btn" Width="80" Height="50"
Command="{Binding onBtnCommand}"></Button>
<TextBlock Text="{Binding Cnt, Mode=TwoWay}"/>
</StackPanel>

</Grid>
</UserControl>
<UserControl x:Class="UiDesktopApp1.Views.User.User"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:UiDesktopApp1.Views.User"
xmlns:vm="clr-namespace:UiDesktopApp1.ViewModels.User"
mc:Ignorable="d"
Background="White"
d:DataContext="{d:DesignInstance local:User}"
d:DesignHeight="120" d:DesignWidth="300">
<UserControl.DataContext>
<vm:UserViewModel/>
</UserControl.DataContext>
<Grid>
<StackPanel>
<Button Content="btn" Width="80" Height="50"
Command="{Binding onBtnCommand}"></Button>
<TextBlock Text="{Binding Cnt, Mode=TwoWay}"/>
</StackPanel>

</Grid>
</UserControl>
JJ
JJOP•8mo ago
public UserViewModel ViewModel { get; }
public User(UserViewModel viewModel)
{

InitializeComponent();
ViewModel = viewModel;
DataContext = this;

}
public UserViewModel ViewModel { get; }
public User(UserViewModel viewModel)
{

InitializeComponent();
ViewModel = viewModel;
DataContext = this;

}
this code is wrong code?
leowest
leowest•8mo ago
yes because the way your useing the usercontrol it cannot be initialized so instead u sets the datacontext via xaml
JJ
JJOP•8mo ago
Actually Im following "WPF-UI" Template . so I thought UserControl also is the same code with Page code
leowest
leowest•8mo ago
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
JJ
JJOP•8mo ago
yea actually I don't know how to use that..
JJ
JJOP•8mo ago
presenter you are saying is this part?
No description
leowest
leowest•8mo ago
<ContentPresenter x:Name="RootContentDialog" Grid.Row="0" />
JJ
JJOP•8mo ago
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...
leowest
leowest•8mo ago
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
JJ
JJOP•8mo ago
is there any tutorial or sample code? resolving using ContentControl.
leowest
leowest•8mo ago
I dont know of any no sorry.
JJ
JJOP•8mo ago
nono, That's already enough answer. thanks!
MODiX
MODiX•8mo ago
Use the /close command to mark a forum thread as answered
JJ
JJOP•8mo ago
I send to you message,
leowest
leowest•8mo ago
send it here, I dont take dms
JJ
JJOP•8mo ago
I want to send a coffee coupon to you. What should I do?
leowest
leowest•8mo ago
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.
JJ
JJOP•8mo ago
thank you, If I become an expert, I will be of help to others like you.
leowest
leowest•8mo ago
u dont need to be an expert to help 😉 I am definitively not one either
Want results from more Discord servers?
Add your server