gpt4 book ai didi

c# - 如何在 wpf 中通过 MVVM 使用 ListView 绑定(bind)获取数据?

转载 作者:太空狗 更新时间:2023-10-29 22:55:28 27 4
gpt4 key购买 nike

我尝试使用 Silverlight ListView 生成 MVVM 模式。但是如果我绑定(bind)数据我的 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>

View 模型:

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>

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

alt text

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

alt text alt text

最佳答案

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

ItemsSource="{Binding}"

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

关于c# - 如何在 wpf 中通过 MVVM 使用 ListView 绑定(bind)获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3861647/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com