0

Hello I'm new here and I'm trying to explain my problem in a good english because it's not my mother tongue :) So I'm using MVVM Light and I have my RelayCommand :

private RelayCommand _SearchMethod;
 public RelayCommand SearchMethod
    {

        get
        {
            return _SearchMethod = _SearchMethod ?? new RelayCommand(GetDataMethod, CanSearchMethod);

        }

    }

And in my GetDataMethod , I retrieve some values that I put in the Record Object . I use try catch because sometimes valueMax1 or valueMax2 have values and sometimes not but when they don't , I put 00.00 .

See below :

public void GetDataMethod() {

        var filtre = Rec.Where(y => y.Date.Contains(SelectedItemCombo1) && y.ID == SelectedItemCombo2.ID).Take(22).ToList();
        var filtre2 = Rec2.Where(y => y.Date.Contains(SelectedItemCombo1) && y.ID == SelectedItemCombo2.ID).Take(22).ToList();             
        var valueMax1 = (double)0;
        var valueMax2 = (double)0;



        try {
                valueMax1 = filtre.Where(x => x.Unite == "MWh" || x.Unite == "kWh" && x.Valeur != "0").
              TakeWhile(y => !y.Valeur.Contains(dateConsommation[1]))
              .Max(y => double.Parse(y.Valeur, CultureInfo.InvariantCulture));
        }
        catch (InvalidOperationException e)
        {
            Console.WriteLine("Pas de valeur pour l'année écoulée");
            valueMax1 = 00.00;

        }finally {

            try
            {

                var filtre3 = filtre2.SkipWhile(y => !y.Valeur.Contains(dateConsommation[1])).Where(x => x.Unite == "MWh" || x.Unite == "kWh").ToList();
                valueMax2 = filtre3.Max(y => double.Parse(y.Valeur, CultureInfo.InvariantCulture));

            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine("Pas de valeur pour l'année précédente");
                valueMax2 = 00.00;
            }
            finally {
                if (Record == null)
                {
                    Record = new FilteredRecord()
                    {
                        Date = SelectedItemCombo1,
                        ID = SelectedItemCombo2.ID,
                        Value1 = (valueMax1 == 00.00) ? 00.00 : valueMax1,
                        Value2 = (valueMax2 == 00.00) ? 00.00 : valueMax2,
                        Value3 = valueMax1 - valueMax2
                    };
                }
                else
                {
                    Record.Date = SelectedItemCombo1;
                    Record.ID = SelectedItemCombo2.ID;
                    Record.Value1 = (valueMax1 == 00.00) ? 00.00 : valueMax1;
                    Record.Value2 = (valueMax2 == 00.00) ? 00.00 : valueMax2;
                    Record.Value3 = valueMax1 - valueMax2;
                }

            }

        }





    } 

My Record has the values that I want .

My question is : Why in the MainWindow.xaml the binding is not working ??? Because before using RelayCommand , the binding worked !!

My code in the MainWindow.xaml :

 <Window x:Class="JackAndJayLight.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="435" Width="435"
    xmlns:local="clr-namespace:JackAndJayLight"
    DataContext="{Binding Source={StaticResource Locator}, Path=Main}">  

 <StackPanel Orientation="Vertical" >
                    <TextBox Width="200" Height="20" Margin="0 2 0 2" IsReadOnly="True" Text="{Binding Record.ID}" />
                    <TextBox Width="200" Height="20" Margin="0 2 0 2" IsReadOnly="True" Text="{Binding Record.Date}" />
                    <TextBox Width="200" Height="20" Margin="0 2 0 2" IsReadOnly="True" Text="{Binding Record.Value1}" />
                    <TextBox Width="200" Height="20" Margin="0 2 0 2" IsReadOnly="True" Text="{Binding Record.Value2}" />
                    <TextBox Width="200" Height="20" Margin="0 2 0 2" IsReadOnly="True" Text="{Binding Record.Value3}" />
                </StackPanel> 

My declaration of the object Record:

public FilteredRecord Record { get; private set; }

If you can help me ... Thanks in advance

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
Debuzz89
  • 3
  • 2
  • Is `FilteredRecord` class implementing `INotifyPropertyChanged`? – Rohit Vats Jun 15 '14 at 15:24
  • @RohitVats Yes my class is like this : public class FilteredRecord : GalaSoft.MvvmLight.ObservableObject {} – Debuzz89 Jun 15 '14 at 15:28
  • If `Record` is null, you are initializing it to new value. Make sure class where this command is declared also implements INPC. – Rohit Vats Jun 15 '14 at 15:30
  • @RohitVats Yes because before using RelayCommand the binding worked , my class MainViewModel is like this : public class MainViewModel : ViewModelBase and ViewModelBase herites of ObservableObject – Debuzz89 Jun 15 '14 at 15:34

1 Answers1

0

You need to RaisePropertyChanged event to notify UI that some property has changed. In your case you need to notify for Record property. Change property syntax to this and see if that helps:

private FilteredRecord record;
public FilteredRecord Record
{
  get
  {
     return record;
  }
  private set
  {
     if(record != value)
     {
        record = value;
        RaisePropertyChanged("Record");
     }
  }
}
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • But I don't understand before it worked without it but now I won't forget for my next projects , thanks again ;) – Debuzz89 Jun 15 '14 at 15:54