C
C#16mo ago
Snitrxm

❔ MVVM

Need help
28 Replies
Snitrxm
SnitrxmOP16mo ago
public class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;

public User? user;

public ICommand ShowUsersCommand { get; set; }

public MainViewModel()
{
ShowUsersCommand = new RelayCommand(ShowUsers, CanShowUsers);
}

private void ShowUsers(object obj)
{
user = new User().GetUsers()[0];
NotifyPropertyChanged("User");
}

private bool CanShowUsers(object parameter)
{
return true;
}

private void NotifyPropertyChanged(string propery)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propery));
}
}
public class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;

public User? user;

public ICommand ShowUsersCommand { get; set; }

public MainViewModel()
{
ShowUsersCommand = new RelayCommand(ShowUsers, CanShowUsers);
}

private void ShowUsers(object obj)
{
user = new User().GetUsers()[0];
NotifyPropertyChanged("User");
}

private bool CanShowUsers(object parameter)
{
return true;
}

private void NotifyPropertyChanged(string propery)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propery));
}
}
I have this viewModel In xaml im biding this way
<StackPanel>
<StackPanel Margin="5" Orientation="Horizontal">
<TextBlock Text="Id: "/>
<TextBlock Text="{Binding User.Id}"/>
</StackPanel>

<StackPanel Margin="5" Orientation="Horizontal">
<TextBlock Text="Name: "/>
<TextBlock Text="{Binding User.Name}"/>
</StackPanel>

<StackPanel Margin="5" Orientation="Horizontal">
<TextBlock Text="Email: "/>
<TextBlock Text="{Binding User.Email}"/>
</StackPanel>
<Button Command="{Binding ShowUsersCommand}" Content="Get Users"/>
</StackPanel>
<StackPanel>
<StackPanel Margin="5" Orientation="Horizontal">
<TextBlock Text="Id: "/>
<TextBlock Text="{Binding User.Id}"/>
</StackPanel>

<StackPanel Margin="5" Orientation="Horizontal">
<TextBlock Text="Name: "/>
<TextBlock Text="{Binding User.Name}"/>
</StackPanel>

<StackPanel Margin="5" Orientation="Horizontal">
<TextBlock Text="Email: "/>
<TextBlock Text="{Binding User.Email}"/>
</StackPanel>
<Button Command="{Binding ShowUsersCommand}" Content="Get Users"/>
</StackPanel>
But when i click in button to executre the command ShowUsersCommand the other infos does not changes
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Snitrxm
SnitrxmOP16mo ago
i didnt know it
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Snitrxm
SnitrxmOP16mo ago
No dataContext for biding
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Snitrxm
SnitrxmOP16mo ago
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainViewModel();
}
}
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainViewModel();
}
}
but I added this
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Snitrxm
SnitrxmOP16mo ago
System.Windows.Data Error: 40 : BindingExpression path error: 'User' property not found on 'object' ''MainViewModel' (HashCode=38376892)'. BindingExpression:Path=User.Id; DataItem='MainViewModel' (HashCode=38376892); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'User' property not found on 'object' ''MainViewModel' (HashCode=38376892)'. BindingExpression:Path=User.Id; DataItem='MainViewModel' (HashCode=38376892); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Snitrxm
SnitrxmOP16mo ago
This what i fount in output I do have the property User
public class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;

public User? User;
public class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;

public User? User;
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Snitrxm
SnitrxmOP16mo ago
get set
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Snitrxm
SnitrxmOP16mo ago
I tried to put it but does not work any
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Snitrxm
SnitrxmOP16mo ago
oh i got it
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Snitrxm
SnitrxmOP16mo ago
im sure i added this before and does not work but okay thx
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Snitrxm
SnitrxmOP16mo ago
you said about a toolkit i didnt know it i'll search about it
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Snitrxm
SnitrxmOP16mo ago
ty appreciate it
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Snitrxm
SnitrxmOP16mo ago
@TeBeClone im implementing the toolkit
[ObservableProperty]
public User? user;
[ObservableProperty]
public User? user;
i did this and it gives me the same error
System.Windows.Data Error: 40 : BindingExpression path error: 'user' property not found on 'object' ''MainViewModel' (HashCode=21520579)'. BindingExpression:Path=user.Id; DataItem='MainViewModel' (HashCode=21520579); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'user' property not found on 'object' ''MainViewModel' (HashCode=21520579)'. BindingExpression:Path=user.Id; DataItem='MainViewModel' (HashCode=21520579); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
but now i cant add { get; set; }
sibber
sibber16mo ago
read the docs
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server