【问题标题】:How to get data using ListView Binding via MVVM in wpf?如何在 wpf 中通过 MVVM 使用 ListView 绑定获取数据?
【发布时间】:2010-10-05 07:31:40
【问题描述】:

我尝试使用 Silverlight ListView 生成 MVVM 模式。但是,如果我绑定数据,我的 silverlight 控件将没有数据可视化。也没有错误返回。我看到了空的gridview。

型号:

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

查看:

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>

视图模型:

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

  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>

我写了这些代码,但我看不到数据。看下面:

我在调试模式下添加了 2 张图片:

【问题讨论】:

    标签: c# .net wpf mvvm


    【解决方案1】:

    据我所知,您将项目列表直接分配为数据上下文,因此您的路径应该是:

    ItemsSource="{Binding}"
    

    列表类上没有属性“MyData”,这是不需要在绑定中指定的数据类型,因为 WPF 框架将使用反射来搜索 MyData 上的所有属性

    【讨论】:

      【解决方案2】:

      您将列表指定为 DataContext,但列表视图中 ItemsSource 的绑定需要一个名为 MyData 的属性。

      您将 datacontext 设置为视图模型并公开包含列表的 MyData 属性您将 ItemsSource 绑定更改为 ItemsSource="{Binding}"

      编辑:好的,在您最近一次编辑之后,您的属性名称和绑定似乎仍然不匹配。您绑定到MyData,但该属性称为myData。 (注意大小写差异)

      【讨论】:

      • 对不起,我没有诚实地复制我的同步答案
      【解决方案3】:

      尝试使用ObservableCollection&lt;MyData&gt; 而不是常规的List&lt;T&gt;。因为绑定发生在列表被填充之前,所以不会通知列表中的更改。
      ObservableCollection 确实会在添加项目时向 Binding 发出通知。

      【讨论】:

      • public ObservableCollection GetData() { ObservableCollection myDataList = new ObservableCollection();
      • 看来我错过了这个错误,但 Isak Savo 确实抓住了它,请查看他的答案。但是,使用 ObservableCollections 进行绑定是一种很好的做法。
      【解决方案4】:

      您需要将 ListViewItemsSource 属性绑定到 ViewModel 的属性

      目前您正在绑定到 MyData,这似乎不是您的 ViewModel 的属性。

      您可能应该在 ViewModel 的构造函数中运行 GetData() 方法,因为它似乎使用默认数据填充列表,并创建如下属性:

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

      并将你们中的ItemsSource 设置为ListView

      更新

      您已将myData 公开为MyDataViewModel 中的公共字段。这与公开属性不同。我上面的例子是一个属性(现在)。

      此外,正如 Roel 所说,您应该使用 ObservableCollection&lt;MyData&gt; 而不是 List&lt;MyData&gt;,这样您就可以看到您的收藏发生的任何变化。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-15
        • 2015-05-24
        • 1970-01-01
        • 1970-01-01
        • 2018-08-14
        • 2011-06-08
        • 1970-01-01
        相关资源
        最近更新 更多