RelayCommand没有在MainViewModel中绑定我的对象

时间:2014-06-15 15:04:29

标签: c# wpf binding mvvm-light relaycommand

您好,我是新来的,我试图以良好的英语解释我的问题,因为它不是我的母语:) 所以我使用MVVM Light,我有我的RelayCommand:

private RelayCommand _SearchMethod;
 public RelayCommand SearchMethod
    {

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

        }

    }

在我的GetDataMethod中,我检索了一些放在Record对象中的值。我使用try catch,因为有时值valueMax1或valueMax2有值,有时候没有,但是当它们没有时,我把00.00。

见下文:

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;
                }

            }

        }





    } 

我的记录包含我想要的值。

我的问题是:为什么在MainWindow.xaml中绑定不起作用?因为在使用RelayCommand之前,绑定工作!!

我在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> 

我对象的声明记录:

public FilteredRecord Record { get; private set; }

如果你能帮助我...提前致谢

1 个答案:

答案 0 :(得分:0)

您需要RaisePropertyChanged事件通知UI某些属性已更改。在您的情况下,您需要通知Record属性。将属性语法更改为此,看看是否有帮助:

private FilteredRecord record;
public FilteredRecord Record
{
  get
  {
     return record;
  }
  private set
  {
     if(record != value)
     {
        record = value;
        RaisePropertyChanged("Record");
     }
  }
}