简体   繁体   English

如何在wpf中通过MVVM使用ListView绑定获取数据?

[英]How to get data using ListView Binding via MVVM in wpf?

i try to use generate MVVM pattern using Silverlight ListView. 我尝试使用Silverlight ListView使用生成MVVM模式。 But if i bind data my silverlight control no data visualize. 但如果我绑定数据我的silverlight控件没有数据可视化。 also no error return. 也没有错误返回。 i see empty gridview. 我看到空的gridview。

Model: 模型:

MyData.cs MyData.cs


 public class MyData
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Price { get; set; }
        public string Author { get; set; }
        public string Catalog { get; set; }
    }

View: 视图:

BookViewer.xaml BookViewer.xaml


<UserControl x:Class="wpf.MVVM.Project.View.BookViewer"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="500" Width="700">
    <Grid>
        <StackPanel>
            <ListView Margin="8" Height="400" Width="650" ItemsSource="{Binding Path=MyData}">
         <ListView.View>
                    <GridView >
                        <GridViewColumn Header="ID" Width="Auto">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding ID}" TextAlignment="Right" Width="40"/>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="Name" Width="100"/>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=Price}" Header="Price" Width="100"/>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=Author}" Header="Author" Width="100"/>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=Catalog}" Header="Catalog" Width="100"/>
                    </GridView>
                </ListView.View>
            </ListView>
        </StackPanel>
    </Grid>
</UserControl>

ViewModel: 视图模型:

MyDataViewModel.cs MyDataViewModel.cs


  public class MyDataViewModel
    {
       // public ObservableCollection<MyData> myData { get; set; }
        public List<MyData> GetData()
        {
            List<MyData> myDataList = new List<MyData>();
            myDataList.Add(new MyData() { ID = 1, Name = "yusuf karatoprak", Author = "Mike Gold", Price = "6.7 TL", Catalog = "IT" });
            myDataList.Add(new MyData() { ID = 2, Name = "yusuf karatoprak", Author = "Scott Gunitella", Price = "9.7 TL", Catalog = "IT" });
            myDataList.Add(new MyData() { ID = 3, Name = "yusuf karatoprak", Author = "David Hayden", Price = "11.7 TL", Catalog = "IT" });
            if (myDataList.Count > 0)
            {
                return myDataList;
            }
            else
                return null;
        }

    }

Window1.xaml Window1.xaml

  public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            MyDataViewModel myDataCtx = new MyDataViewModel();
            MyDataDataView.DataContext = myDataCtx.GetData();
        }
    }

<Window x:Class="wpf.MVVM.Project.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="1000" Width="900"
    xmlns:views="clr-namespace:wpf.MVVM.Project.View" Loaded="Window_Loaded">
    <Grid>
        <views:BookViewer x:Name="MyDataDataView" Margin="0,0,0,0"></views:BookViewer>
    </Grid>
</Window>

i writed these codes but no data i can see.Look below: 我写了这些代码,但没有我能看到的数据。看下面:

替代文字

i added 2 pics my debug mode: 我添加了2个图片我的调试模式:

替代文字替代文字

As far as I can see you are assigning the list of items directly as your data context therefore your path should just be: 据我所知,您直接将项目列表分配为数据上下文,因此您的路径应该是:

ItemsSource="{Binding}"

There is no property "MyData" on the list class, this is the datatype which does not need to be specified in the binding as the WPF framework will use reflection to search for all of the properties on MyData 列表类中没有属性“MyData”,这是不需要在绑定中指定的数据类型,因为WPF框架将使用反射来搜索MyData上的所有属性

You assign the list as DataContext but the binding for ItemsSource in your listview expects a property named MyData . 您将列表指定为DataContext,但listview中ItemsSource的绑定需要名为MyData的属性。

Either you set the datacontext to the viewmodel and expose a MyData property containing the list or you change your ItemsSource binding to just ItemsSource="{Binding}" . 您可以将datacontext设置为viewmodel并公开包含该列表的MyData属性, 或者将ItemsSource绑定更改为ItemsSource="{Binding}"

EDIT: Ok, after your latest edit it still seems you have a mismatch between property name and binding. 编辑:好的,在您最近的编辑之后,似乎您的属性名称和绑定之间仍然不匹配。 You bind to MyData but the property is called myData . 绑定到MyData但属性名为myData (notice case difference) (注意案例差异)

Try using an ObservableCollection<MyData> instead of a regular List<T> . 尝试使用ObservableCollection<MyData>而不是常规List<T> Because the binding occurs before the list is filled, it is not notified of changes in the list. 由于绑定发生在列表填充之前,因此不会通知列表中的更改。
The ObservableCollection does give notifications to the Binding when an item is added. 添加项目时,ObservableCollection会向Binding发出通知。

You need to bind the ItemsSource proprety of your ListView to a property of your ViewModel 您需要将ListViewItemsSource属性绑定到ViewModel的属性

At the moment you are binding to MyData which doesn't appear to be a property of your ViewModel. 目前您绑定到MyData ,它似乎不是您的ViewModel的属性。

You should probably run your GetData() method in the constructor of your ViewModel, as it appears to populate a list with default data, and create a property like: 您可能应该在ViewModel的构造函数中运行GetData()方法,因为它似乎用默认数据填充列表,并创建一个属性,如:

IEnumerable<MyData> MyDataList
{
    get{return myData;}
}

and set the ItemsSource of you ListView to that. 并将ListViewItemsSource设置为。

Update 更新

You have exposed myData as a public field in MyDataViewModel . 您已将myData公开为MyDataViewModel的公共字段。 This is not the same as exposing a property. 这与公开财产不同。 My above example is a property (now). 我上面的例子是一个属性(现在)。

Also, as Roel says, you should use an ObservableCollection<MyData> rather than a List<MyData> so that you can see any changes to your collection, as they happen. 此外,正如Roel所说,您应该使用ObservableCollection<MyData>而不是List<MyData>这样您就可以看到对集合的任何更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM