alice7
alice7

Reputation: 3884

Use MVVM for WPF dialog box

I want to use MVVM in my WPF application. I currently have a Model and a view which has DataGrid and some other controls. I created a ViewModel based on my model and don't know if I did it correctly. The view is just a simple dialog box. I want to fill the DataGrid view.

How can I tell the DataGrid to bind with the ViewModel?

I would like to bind properties(inside viewmodel like ID and Date) to the datagrid.

SO like if there is two objects inside the list I would like to see two rows in datagrid with the specific ID's and Date's.

Im setting the datacontext inside the class instead xaml.

Here is the code so far:

public class ViewModel : INotifyPropertyChanged
{
    private string _id;
    private DateTime _date;
    private ObservableCollection<Object> _list;

    public string Id
    {
        get { return _id; }
        set
        {
            _id = value;
            PropertChanged("Id");
        }
    }
    public DateTime Date
    {
        get { return _date; }
        set
        {
            _date = value;
            PropertChanged("Date");
        }
    }
    public ObservableCollection<Object> list
    {
        get { return _list; }
        set
        {
            _list = value;
            PropertChanged("list");
        }
    }

    public LicenseViewModel()
    {
        list = GetList();
    }
    public event PropertyChangedEventHandler PropertyChanged;

    public void PropertChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

And the XAML:

<Window x:Class="Import"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
      mc:Ignorable="d"
      ResizeMode="CanResizeWithGrip"
      x:Name="ImportLicense"
      d:DesignHeight="493" d:DesignWidth="559"
      Title="Import Licenses" SizeToContent="WidthAndHeight">

    <Grid Width="538">
        <DataGrid x:Name="Imported" VerticalAlignment="Top"  AutoGenerateColumns="False" CanUserResizeColumns="True">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Entitlement ID" Binding="{Binding Path=ID}"/>
                <DataGridTextColumn Header="Date Sold" Binding="{Binding Path=Date}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

Upvotes: 1

Views: 1915

Answers (3)

Craig Suchanec
Craig Suchanec

Reputation: 10804

You need to set the data context for the DataGrid to the instance of your view model. You can do this by simply setting the DataContext of your View or the DataGrid to your instance of your view model in the constructor of the view class. This is a quick and dirty way of doing this.

If you want to be more sophisticated you can create a DepenencyProperty on your view class like this:

public static DependencyProperty ViewModelProperty =
        DependencyProperty.Register("ViewModel",
        typeof(ItemViewModel),
        typeof(ViewClassHere));

public ItemViewModel ViewModel
{
    get { return (ItemViewModel)base.GetValue(ItemViewModel); }
    set { base.SetValue(ItemViewModel, value); }
}

Then you'd bind to that property in any one of many ways but one way would be like:

<DataGrid  ItemsSource="{Binding ElementName=windowName, Path=viewName.list}">

There are a bunch of ways to do this, these are just two possible ways to do this.

Upvotes: 2

Make It Perfect
Make It Perfect

Reputation: 995

Try like this:

<window.Resources>             
<ViewModel  x:Key="ViewModel"></ViewModel >
</window.Resources>
<Grid  x:Name="ValueDetail"  DataContext="{StaticResource ViewModel}">
<DataGrid ItemsSource="{Binding MyCollection}"/> 
</Grid>

Upvotes: 0

blindmeis
blindmeis

Reputation: 22445

the common way to display data with a datagrid is to set the itemssource

 <DataGrid ItemsSource="{Binding MyCollection}"/>

your viewmodel defines 2properties and one collection, but in your xaml you bind your properties to the datagrid columns and don't set any itemssource.

its not clear to me what you would like to see in your datagrid, but your 2 properties ID and DateTime are not part of any collection, so why you want this to display in your datagrid?

please edit your question and give some information of what you wanna see in your datagrid.

Upvotes: 0

Related Questions