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