0

I have model called ListViewModel with one method:

namespace LayoutMVVM.ViewModels
{
    public class ListViewModel
    {
        public void getData()
        {
        testViewClassDataContext tv = new testViewClassDataContext();
        List<test_view> tvq = (from tt in tv.test_views
                               select tt).ToList();
        }
    }
}

Into my UserControl called ListView I want to get result from model and bind to List:

ListView.xaml:

<UserControl .....>    
    <Grid Background="Crimson">

        <ListView Width="230" Height="250"  Name="lvMyList">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="FirstName" Width="90" DisplayMemberBinding="{Binding FirstName}" />
                    <GridViewColumn Header="LastName" Width="90" DisplayMemberBinding="{Binding LastName}" />
                    <GridViewColumn Header="Type" Width="50" DisplayMemberBinding="{Binding Type}" />
                </GridView>
            </ListView.View>
        </ListView>

    </Grid>
</UserControl>

ListView.cs

namespace LayoutMVVM.Views
{
    public partial class ListView : UserControl
    {
        public ListView()
        {
            InitializeComponent();

            ListViewModel lvm = new ListViewModel();
            lvMyList.ItemsSource = lvm.getData(); //error 
        }
    }
}

I'm starting with WPF and don't know how to correclty bind data.

0

3 Answers 3

4

Probably you should change the method as follows, so that method will return actual results,

public List<test_view> getData()
    {
        testViewClassDataContext tv = new testViewClassDataContext();
        List<test_view> tvq = (from tt in tv.test_views
                               select tt).ToList();
        return tvq;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

@@Sajeetharan - thanks, one question: it's ok with MVVM or should I change something?
Yeah it's totally fine
thanks, because Naresh wrote to use this.DataContext = lvm....instead of my lvMyList.ItemsSource = lvm.getData();
1

Sajeetharan's answer looks fine but if you want to go ahead with MVVM, replace code behind code like this:

namespace LayoutMVVM.Views
{
    public partial class ListView : UserControl
    {
        public ListView()
        {
            InitializeComponent();
            ListViewModel lvm = new ListViewModel();
            this.DataContext = lvm; //this is what you are missing                
        }
    }
}

and assign ItemsSouce of ListView in XAML like this:

        <ListView Width="230" Height="250"  Name="lvMyList" ItemsSource="{Binding tvq}" >
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="FirstName" Width="90" DisplayMemberBinding="{Binding FirstName}" />
                    <GridViewColumn Header="LastName" Width="90" DisplayMemberBinding="{Binding LastName}" />
                    <GridViewColumn Header="Type" Width="50" DisplayMemberBinding="{Binding Type}" />
                </GridView>
            </ListView.View>
        </ListView>

You will have to create a list property List(or ObservableCollection) "tvq" and populate it by calling getData() in ViewModel.

I would suggest you read more about MVVM and bindings. Happy coding !!

7 Comments

He is setting directly the itemsource and so, if the getdata() method would return something, it should work anyway without setting the datacontext.
@CiccioRocca : Got your point. Bt anyways if he wants to go with MVVM, I think my answer would be useful.
Yes it is a good suggestion, but the reason his program doesn't work is not related to DataContext, but it's the fact that getData method is a Void and so the line :lvMyList.ItemsSource = lvm.getData(); doesn't make any sense.
so how can I use data his.DataContext = lvm to load data? as now after I change method as Sajeetharan said I using lvMyList.ItemsSource = lvm.getData();
@4est Now that you've changed the getData as told you Sajeetharan, you could set Itemssource with Databinding as suggested Naresh Ravlani. Merge the two answers ;)
|
0

I'm adding this response to integrate what have been told in the previous response:

First of all change the method getData as told you Sajeetharan:

public List<test_view> getData()
    {
        testViewClassDataContext tv = new testViewClassDataContext();
        List<test_view> tvq = (from tt in tv.test_views
                               select tt).ToList();
        return tvq;
    }

The second thing to do is changing code behind as told you Naresh Ravlani:

public partial class ListView : UserControl
    {
        public ListView()
        {
            InitializeComponent();
            ListViewModel lvm = new ListViewModel();
            this.DataContext = lvm; //this is what you are missing                
        }
    }

Third pay attention that the equivalent you have also tell to your view that the list has an itemssource:

        <ListView Width="230" Height="250"  Name="lvMyList" ItemsSource="{Binding}" >
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="FirstName" Width="90" DisplayMemberBinding="{Binding FirstName}" />
                    <GridViewColumn Header="LastName" Width="90" DisplayMemberBinding="{Binding LastName}" />
                    <GridViewColumn Header="Type" Width="50" DisplayMemberBinding="{Binding Type}" />
                </GridView>
            </ListView.View>
        </ListView>

Look at this:

<ListView Width="230" Height="250"  Name="lvMyList" ItemsSource="{Binding}" >

Note that you have to tell to the listview to take its itemsource from Datacontext using DataBinding (ItemSource = "{Binding}").

I hope it will help you!

1 Comment

@@CiccioRocca it's working without this.DataContext = lvm;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.