Frage

Ich habe eine einfache MVVM, mit nur drei Klassen CashFlowView, CashFlowViewModel, CashFlowModel erstellt.

Ich verwende einen 9.1 XamDataPresenter des Infragistic (oder xamDataGrid).

    <igDP:XamDataPresenter Name="xamDataPresenter1" DataSource="{Binding Source={StaticResource CashFlowData}}">

    <ObjectDataProvider x:Key="CashFlowData" ObjectType="{x:Type ViewModel:CashflowViewModel}" MethodName="GetCashFlows" />

In meinem Ansichtsmodell:

public ObservableCollection<CashflowModel> GetCashFlows()
        {
            return new ObservableCollection<CashflowModel>() { ... };
        }

Ansichtsmodell ist mit Blick durch diese:

this.DataContext = new CashflowViewModel();

Solange ich das Raster der Object seine perfekt läuft gut verbinden. Aber ich wünschte, ich könnte nur statt auf eine Eigenschaft in meinem Ansichtsmodell verbinden.

Nach Infragistics alles, was ich tun muß, ist dies:

<igDP:XamDataGrid DataSource="{Binding Path=ViewModelCollection}"/>

Aber in diesem Fall scheint es, ich zu binden muß zu einer Sammlung von anderem Ansichtsmodell meiner Reihen innerhalb des Gitters darstellen. Und das ist, wo ich verwirrt.

Ich habe versucht und es funktioniert nicht:

<igDP:XamDataPresenter Name="xamDataPresenter1" DataSource="{Binding Path=CashFlows}">

Im Innern des Ansichtsmodell:

public ObservableCollection<CashflowDataGridViewModel> CashFlows
        {
            get
            {
                return new ObservableCollection<CashflowDataGridViewModel>();
            }
        }

Aber wie erstelle ich mein zweites Viewmodel (CashflowDataGridViewModel)?

Ich habe versucht, das Hinzufügen dieses proprty innerhalb dieser zweiten Ansichtsmodell:

public CashflowModel CashFlow
        {
            get
            {
                return new CashflowModel() {...};
            }
        }

Aber alles, was ich in meiner Ansicht gezeigt bekommen ist „Cashflow“ Spaltenkopf, ohne dass die zugrunde liegenden Header der aktuellen cashflowModel Klasse.

War es hilfreich?

Lösung

To be able to bind the View to properties on the ViewModel, the DataContext needs to be set to an instance of your ViewModel. What I commonly do is to include the following line in the constructor of the code-behind for my View:

this.DataContext = new SomeAwesomeViewModel();

You can also set the DataContext for containers if you want different groups of controls to use different ViewModels (e.g., Grid.DataContext, StackPanel.DataContext, etc.).

Once you have the DataContext set, you should be able to bind to the properties of that ViewModel.

Update

Here's a bit of sample code to get you going.

public class CashFlowViewModel
{
    public ObservableCollection<FlowViewModel> DataGridData
    {
        get...
    }
}

That's the property which should provide the data for the DataGrid. Now, here's what the FlowViewModel class could look like.

public class FlowViewModel
{
    decimal flowAmount;
    public decimal FlowAmount
    {
        get { return flowAmount; }
        set
        {
            if(flowAmount == value)
                return;

            flowAmount = value;
            NotifyPropertyChanged("FlowAmount");
        }
    }
    .
    .
    .

    private void NotifyPropertyChanged(string propertyName)
    {
        if(PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top