My viewmodel doesn't work...

c00012 716 Reputation points
2021-06-29T13:05:16.75+00:00

Hello,

I have an question on passing values between viewmodels.

I have two viewmodels. one is to navigate between views (these views are usercontrols), and the other is to do something with the values from view(view has several textboxes) and combobox.

ViewModel 1(navigating)

using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace PaymentEst.Viewmodels
{
class MainViewModel:ObservableObject
{
//code for navigation
public ICommand ProdSelectCommand { get; set; }

    private IList<string> _prodStrings = new List<string>();  
    public IList<string> ProdStrings  
    {  
        get => _prodStrings;  
        set => SetProperty(ref _prodStrings, value);  
    }  

    private string _selectedCode;  
    public string SelectedCode  
    {  
        get => _selectedCode;  
        set => SetProperty(ref _selectedCode, value);  
    }  
    public PersonalLoanViewModel PersonalLoanVM { get; }  
     
    public MainViewModel()  
    {  
        foreach (object item in Enum.GetValues(typeof(ProdCode)))  
        {  
            ProdStrings.Add(item.ToString());  
        }  
        ProdSelectCommand = new RelayCommand<object>(p => ShowSelect(p));  
        PersonalLoanVM = new PersonalLoanViewModel();  
    }  

    private void ShowSelect(object obj)  
    {  
        if (obj is not SelectionChangedEventArgs args)  
        {  
            return;  
        }  

        object SelectedItem = args.AddedItems[0];  
        SelectedCode = SelectedItem.ToString();  
    }  
}  

}

ViewModel 2:

using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;
using System;
using System.Windows;
using System.Windows.Input;

namespace PaymentEst.Viewmodels
{
class PersonalLoanViewModel:ObservableObject
{
public ICommand PLCommand { get; set; }

    private string _prod;  
    public string Prod  
    {  
        get => _prod;  
        set => SetProperty(ref _prod, value);  
    }  

    private string _lender;  
    public string Lender  
    {     
        get => _lender;  
        set => SetProperty(ref _lender, value);  
    }  

    private string _loanamt;  
    public string LoanAmt  
    {  
        get => _loanamt;  
        set => SetProperty(ref _loanamt, value);  
    }  

    private string _apr;  
    public string APR  
    {  
        get => _apr;  
        set => SetProperty(ref _apr, value);  
    }  

    private string _terms;  
    public string Terms  
    {  
        get => _terms;  
        set => SetProperty(ref _terms, value);  
    }  

    public PersonalLoanViewModel()  
    {  
        PLCommand = new RelayCommand(ShowOffer);  
    }  
      

    private void ShowOffer()  
    {  
        var factor = Convert.ToDouble(APR) / 100 / 12;  
        var fogr = Math.Pow(1 + factor, Convert.ToInt32(Terms) * 12);  
        var Payment = Convert.ToInt32(LoanAmt) * factor * (fogr / (fogr - 1));  
        MessageBox.Show(string.Format($"Product: {Prod}, Lender:{Lender}, Amount: ${Convert.ToInt32(LoanAmt):N0}, APR: {APR}%, Terms: {Convert.ToInt32(Terms) * 12} months,  Payment:{Payment:N0} "), "Review your Offer");  
    }  
}  

}

Converter(to pass the values to viewmodel 2)

using System;
using System.Globalization;
using System.Windows.Data;

namespace PaymentEst.Viewmodels
{
class TextBox
{
public string FirstBoxValue { get; set; }
public string SecondBoxValue { get; set; }
public string ThirdBoxValue { get; set; }
public string FourthBoxValue { get; set; }
public string FifthBoxValue { get; set; }
}
class PersonalLoanConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return new TextBox
{
FirstBoxValue = values[0].ToString(),
SecondBoxValue = values1.ToString(),
ThirdBoxValue = values[2].ToString(),
FourthBoxValue = values[3].ToString(),
FifthBoxValue = values[4].ToString()
};
}

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)  
    {  
        throw new NotImplementedException();  
    }  
}  

}

I run these codes and got the error:
110274-ice-screenshot-20210629-164732.png

I can't figure out what is wrong. if someone pick the error, I would be very appreciated.
thanks,

c00012

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,666 questions
{count} votes

Accepted answer
  1. DaisyTian-1203 11,616 Reputation points
    2021-07-01T06:18:05.59+00:00

    I update two xaml of your code.

    MainWindow.xaml: Update getPayment Button as:

    <Button Grid.Row="2" x:Name="getPayment" DataContext="{Binding}"  Command="{Binding PersonalLoanVM.ProdInputCommand}" Content="Review Detail"</Button>  
    

    PersonalLoan.xaml :

     <Grid>  
            <Grid.ColumnDefinitions>  
                <ColumnDefinition Width="*"/>  
                <ColumnDefinition Width="1.5*"/>  
            </Grid.ColumnDefinitions>  
            <Grid.RowDefinitions>  
                <RowDefinition Height="*"/>  
                <RowDefinition Height="*"/>  
                <RowDefinition Height="*"/>  
                <RowDefinition Height="*"/>  
            </Grid.RowDefinitions>  
            <TextBlock Grid.Row="0" Grid.Column="0" Text="Lender:  " VerticalAlignment="Center" HorizontalAlignment="Right" FontSize="24" FontWeight="Bold" />  
            <TextBox x:Name="Lender" Text="{Binding PersonalLoanVM.Lender, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"   Grid.Row="0"  Grid.Column="1" Margin="10" Height="30" FontFamily="Segoe UI" FontSize="18"/>  
            <TextBlock Grid.Row="1" Grid.Column="0" Text="Amount:  " VerticalAlignment="Center" HorizontalAlignment="Right" FontSize="24" FontWeight="Bold" />  
            <TextBox x:Name="Amount" Text="{Binding PersonalLoanVM.LoanAmt, Mode=TwoWay}" Grid.Row="1"  Grid.Column="1" Margin="10" Height="30" FontSize="18" FontFamily="Segoe UI"/>  
            <TextBlock Grid.Row="2" Grid.Column="0" Text="APR:  " VerticalAlignment="Center" HorizontalAlignment="Right" FontSize="24" FontWeight="Bold"/>  
            <TextBox x:Name="APR"  Text="{Binding PersonalLoanVM.APR, Mode=TwoWay}"  Grid.Row="2" Grid.Column="1" Margin="10" Height="30" FontSize="18" FontFamily="Segoe UI"/>  
            <TextBlock Grid.Row="3" Grid.Column="0" Text="Terms:  " VerticalAlignment="Center" HorizontalAlignment="Right" FontSize="24" FontWeight="Bold"/>  
            <TextBox x:Name="Terms" Text="{Binding PersonalLoanVM.Terms, Mode=TwoWay}"  Grid.Row="3" Grid.Column="1" Margin="10" Height="30" FontFamily="Segoe UI" FontSize="18"/>  
        </Grid>  
    

    The result picture:
    110891-3.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful